Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我的输入一直以字符串而不是int形式读取如何解决此问题_Python_Python 3.x - Fatal编程技术网

Python 我的输入一直以字符串而不是int形式读取如何解决此问题

Python 我的输入一直以字符串而不是int形式读取如何解决此问题,python,python-3.x,Python,Python 3.x,所以这个程序的目标是输出最大数量的符合金额的美元钞票,然后是最大数量的百美元,五十美元钞票, 然后是20、10、5和1。在此之后,显示最大的四分之一硬币数、一角硬币数、五分镍币数和一分硬币数 例如,100美元可以显示为10000便士,或2张50美元钞票或5张20美元钞票。但正确的答案是100美元钞票的最大数量:1 100美元钞票。如果面额不为零,则仅显示面额 我遇到的这个问题是,我的输入一直以字符串而不是int形式读取。我如何解决这个问题 amount = input ("enter amou

所以这个程序的目标是输出最大数量的符合金额的美元钞票,然后是最大数量的百美元,五十美元钞票, 然后是20、10、5和1。在此之后,显示最大的四分之一硬币数、一角硬币数、五分镍币数和一分硬币数

例如,100美元可以显示为10000便士,或2张50美元钞票或5张20美元钞票。但正确的答案是100美元钞票的最大数量:1 100美元钞票。如果面额不为零,则仅显示面额

我遇到的这个问题是,我的输入一直以字符串而不是int形式读取。我如何解决这个问题

amount = input  ("enter amount: ")

hundredDollar = amount / 100
amount = amount % 100

fiftyDollar = amount / 50
amount = amount % 50

twentyDollar = amount / 20
amount = amount % 20

tenDollar = amount / 10
amount = amount % 10

fiveDollar = amount / 5
amount = amount % 5

oneDollar = amount / 1
amount = amount % 1

quarter = amount / .25
amount = amount % .25

dime = amount / .10
amount = amount % .10

nickel = amount / .05
amount = amount % .05

penny = amount / .01
amount = amount % .01

print(int(hundredDollar) + " hundred dollar bills")
print(int(fiftyDollar) + " fifty dollar bills")
print(int(twentyDollar) + " twenty dollar bills")
print(int(tenDollar) + " ten dollar bills")
print(int(fiveDollar) + " five dollar bils")
print(int(oneDollar) + " one dollar bills")
print(int(quarter) + " quarters")
print(int(dime) + " dimes ")
print(int(nickel) + " nickels")
print(int(penny) + " pennies")
它将字符串从输入转换为int

如果你想浮动

amount = eval(input  ("enter amount: "))

您可以使用内置函数
int()
float()
分别将字符串作为int或float返回(如果适用)

例如:

amount = float(input  ("enter amount: "))
amount = float(input("Enter amount:"))
hundredDollar = amount // 100
str(hundredDollar) + " hundred dollar bills."
将金额设置为根据用户输入构造的浮点值

其他改进

查看您提供的代码,您可以做的其他改进如下:

使用
/
对数字进行分割和分配

例如:

amount = float(input  ("enter amount: "))
amount = float(input("Enter amount:"))
hundredDollar = amount // 100
str(hundredDollar) + " hundred dollar bills."
将“百分位”设置为整数,表示100进入金额的最大次数。因此,如果金额为150,则百美元将设置为1,因为该金额由一整张百美元钞票组成

将数字与字符串串联时使用
str()

当您将一个数字与一个字符串连接(组合)并且该数字位于第一位时,您需要首先将该数字转换为字符串。例如:

amount = float(input  ("enter amount: "))
amount = float(input("Enter amount:"))
hundredDollar = amount // 100
str(hundredDollar) + " hundred dollar bills."
如果使用浮点,并且希望输出显示为int,即2而不是2.0,则可以使用int()函数或格式化输出。例如:

amount = float(input  ("enter amount: "))
amount = float(input("Enter amount:"))
hundredDollar = amount // 100
str(hundredDollar) + " hundred dollar bills."
为用户输入添加验证


当收到用户的输入时,建议添加一些验证,以检查用户输入的数据是否如预期的那样-在这种情况下,是有效的数量。这可以通过使用
try
来完成,数据类型和
if
语句的
块除外,以检查数据是否在有效范围内或是否满足其他要求。

您的输入之所以一直以
str
ing而不是
int
读取,是因为
input()
返回一个字符串对象(自从他们从Python 2中删除了
raw\u input()
函数并替换了
input()
函数以来,情况一直如此)

使用
int()
函数将字符串更改为整数,如下所示:

print( int(hundredDollar), "hundred dollar bills." ) 
(这也适用于
float()
函数。)

但是,如果用户输入字符串,则会产生错误。要避免这种情况,请在
try
..
块中将转换包装为整数,但
块除外:

amount = int(input("Enter amount: "))

(这同样适用于
float()
函数)

问题在输入行;其他细节是多余的。Eval不“将字符串转换为int”。它执行作为python表达式输入的字符串。现在,如果用户键入一个数字,那么您将返回一个数字-但他们也可以做其他各种事情。在彻底清理参数之前,不要调用eval。对于问题,int是合适的。float不能与//一起使用,运算符/将生成一个float值,而不是所需的int。print(百美元,“百美元钞票”)是simpler@ArpadHorvath我认为浮动汇率更适合在这里使用货币,因为在这个问题中,它表示一个金额可以有便士、镍币、,etc.Float将使用//并返回所需的整数。就输出而言,浮点可以格式化为int。@ArpadHorvath打印时,您对打印语句的建议是合适的。我所建议的方法在将数字与字符串连接在一起的所有情况下都是必需的。我已经更新了答案,以更清楚地反映这一点。实际上,float和int都不适合与currency一起使用。正确的类型是fixedpoint,在Python中可以通过
decimal
访问它。