相关疑难解决方法(0)

如何在Python中将字符串解析为float或int?

在Python中,我如何解析数字字符串,如"545.2222"相应的浮点值,545.2222?或者将字符串解析为"31"整数,31

我只是想知道如何将一个浮点数 解析str为a float,并且(单独)将一个int 解析str为一个int.

python floating-point parsing integer type-conversion

2108
推荐指数
21
解决办法
371万
查看次数

如何检查字符串是否为数字(浮点数)?

检查字符串是否可以在Python中表示为数字的最佳方法是什么?

我目前拥有的功能是:

def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        return False
Run Code Online (Sandbox Code Playgroud)

这不仅是丑陋而且缓慢,似乎很笨重.但是我没有找到更好的方法,因为调用floatmain函数更糟糕.

python floating-point casting type-conversion

1519
推荐指数
21
解决办法
127万
查看次数

在Python中检查类型的规范方法是什么?

检查给定对象是否属于给定类型的最佳方法是什么?如何检查对象是否继承给定类型?

假设我有一个对象o.我如何检查它是否是一个str

python types

1171
推荐指数
12
解决办法
80万
查看次数

询问用户输入,直到他们给出有效的响应

我正在编写一个必须接受用户输入的程序.

#note: Python 2.7 users should use `raw_input`, the equivalent of 3.X's `input`
age = int(input("Please enter your age: "))
if age >= 18: 
    print("You are able to vote in the United States!")
else:
    print("You are not able to vote in the United States.")
Run Code Online (Sandbox Code Playgroud)

如果用户输入合理数据,这将按预期工作.

C:\Python\Projects> canyouvote.py
Please enter your age: 23
You are able to vote in the United States!
Run Code Online (Sandbox Code Playgroud)

但如果他们犯了错误,那就崩溃了:

C:\Python\Projects> canyouvote.py
Please enter your age: dickety six
Traceback (most recent call last):
  File "canyouvote.py", line 1, in …
Run Code Online (Sandbox Code Playgroud)

python validation loops user-input python-3.x

523
推荐指数
10
解决办法
42万
查看次数

input()error - NameError:name'...'未定义

我尝试运行这个简单的python脚本时收到错误:

input_variable = input ("Enter your name: ")
print ("your name is" + input_variable)
Run Code Online (Sandbox Code Playgroud)

让我说我输入"dude",我得到的错误是:

line 1, in <module>
input_variable = input ("Enter your name: ")
File "<string>", line 1, in <module>
NameError: name 'dude' is not defined
Run Code Online (Sandbox Code Playgroud)

我正在运行Mac OS X 10.9.1,我正在使用安装python 3.3附带的Python Launcher应用程序来运行脚本.

编辑:我意识到我用2.7运行这些脚本.我想真正的问题是我如何使用3.3版运行我的脚本?我想如果我将我的脚本拖放到我的应用程序文件夹中的Python 3.3文件夹内的Python Launcher应用程序之上,它将使用3.3启动我的脚本.我想这个方法仍然用2.7启动脚本.那么我如何使用3.3?

python input nameerror python-2.7

193
推荐指数
8
解决办法
60万
查看次数

为什么我得到TypeError:不能将序列乘以'float'类型的非int?

我打字以获得销售额(按输入)乘以定义的销售税(0.08),然后打印总金额(销售税时间销售金额).

我遇到了这个错误.谁知道什么可能是错的或有任何建议?

salesAmount = raw_input (["Insert sale amount here \n"])
['Insert sale amount here \n']20.99
>>> salesTax = 0.08
>>> totalAmount = salesAmount * salesTax

Traceback (most recent call last):
  File "<pyshell#57>", line 1, in <module>
    totalAmount = salesAmount * salesTax
TypeError: can't multiply sequence by non-int of type 'float'
Run Code Online (Sandbox Code Playgroud)

python

80
推荐指数
2
解决办法
28万
查看次数

TypeError:不支持的操作数类型 - :'str'和'int'

新的python和编程我怎么会得到这个错误?

def cat_n_times(s, n):
    while s != 0:
        print(n)
        s = s - 1

text = input("What would you like the computer to repeat back to you: ")
num = input("How many times: ")

cat_n_times(num, text)
Run Code Online (Sandbox Code Playgroud)

python python-3.x

47
推荐指数
2
解决办法
31万
查看次数

TypeError:'str'和'int'实例之间不支持'<='

我正在学习python并且正在练习练习.其中之一是编码投票系统,使用列表选择比赛的23名球员之间的最佳球员.

我正在使用Python3.

我的代码:

players= [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
vote = 0
cont = 0

while(vote >= 0 and vote <23):
    vote = input('Enter the name of the player you wish to vote for')
    if (0 < vote <=24):
        players[vote +1] += 1;cont +=1
    else:
        print('Invalid vote, try again')
Run Code Online (Sandbox Code Playgroud)

我明白了

TypeError:'str'和'int'实例之间不支持'<='

但我这里没有任何字符串,所有变量都是整数.

python python-3.x

27
推荐指数
2
解决办法
25万
查看次数

python if elif else声明

我正在尝试使用python创建一个计算运费的程序.

但是,我无法将程序运行到正常工作的位置.

我的总金额相同,美国为6美元,加拿大为8美元.我似乎无法通过那个.

total = raw_input('What is the total amount for your online shopping?')
country = raw_input('Shipping within the US or Canada?')

if country == "US":
    if total <= "50":
        print "Shipping Costs $6.00"
    elif total <= "100":
            print "Shipping Costs $9.00"
    elif total <= "150":
            print "Shipping Costs $12.00"
    else:
        print "FREE"

if country == "Canada":
    if total <= "50":
        print "Shipping Costs $8.00"
    elif total <= "100":
        print "Shipping Costs $12.00"
    elif total <= "150":
        print "Shipping Costs …
Run Code Online (Sandbox Code Playgroud)

python if-statement nested

20
推荐指数
4
解决办法
10万
查看次数

"不可解决的类型:int()<str()"

我正试图在Python上制作退休计算器.语法没有问题,但是当我运行以下程序时:

def main():
    print("Let me Retire Financial Calculator")
    deposit = input("Please input annual deposit in dollars: $")
    rate = input ("Please input annual rate in percentage: %")
    time = input("How many years until retirement?")
    x = 0
    value = 0
    while (x < time):
        x = x + 1
        value = (value * rate) + deposit
        print("The value of your account after" +str(time) + "years will be $" + str(value))
Run Code Online (Sandbox Code Playgroud)

它告诉我:

Traceback (most recent call last):
  File "/Users/myname/Documents/Let Me Retire.py", …
Run Code Online (Sandbox Code Playgroud)

python calculator

17
推荐指数
1
解决办法
10万
查看次数