python 2.7.10运动我无法弄清楚

时间:2015-06-25 22:27:01

标签: python python-2.7

所以即时学习如何用python编程,最近就开始了,我还是很糟糕!有这个练习要求我创建一个程序,广告你的变化,询问5c,10c,20c和50c硬币..我不知道为什么它不工作的总数是高得离谱,有人可以帮忙吗?

print "Hello mate, this programme helps you calculate the amount of small change you",
print "carry in Euros, if you are too dumb to count it on your own or",
print "just too lazy, this is the programme for you!!"


q=10*raw_input("how many 10c coins do you have?")
d=20*raw_input("how many 20c coins do you have?")
n=5*raw_input("how many 5c coins do you have?")
p=50*raw_input("how many 50c coins do you have?")`enter code here`
tc=int(q+d+n+p)

print "your total change is",tc,"thank you for choosing this programme!" 

6 个答案:

答案 0 :(得分:1)

Python有两个用于读取用户输入的函数,称为inputraw_input。 raw_input不会以字符串格式评估数据并按原样返回数据。而输入函数评估值。因此,为了让您的输入被重新识别为整数,我建议您使用

q = 10 * input("how many 10c coins do you have?")

有关它的更详细解释,请参阅How can I read inputs as integers?

或参考python docs https://docs.python.org/2/library/functions.html#input

答案 1 :(得分:0)

raw_input以字符串形式读取您的数字,因此您需要在将它们相乘之前将它们转换为整数。你正在做的乘法是重复数等于硬币值的次数。

答案 2 :(得分:0)

您必须添加int(raw_input("..."))

print "Hello mate, this programme helps you calculate the amount of small change you",
print "carry in Euros, if you are too dumb to count it on your own or",
print "just too lazy, this is the programme for you!!"


q=10*int(raw_input("how many 10c coins do you have?"))
d=20*int(raw_input("how many 20c coins do you have?"))
n=5*int(raw_input("how many 5c coins do you have?"))
p=50*int(raw_input("how many 50c coins do you have?"))
tc=int(q+d+n+p)

print "your total change is",tc,"thank you for choosing this programme!" 

答案 3 :(得分:0)

这一行:

raw_input("how many 10c coins do you have?")

返回一个字符串。在python中,int,x和字符串s的乘积是重复x次的字符串。您希望在乘法之前将raw_input的输出转换为int。

答案 4 :(得分:0)

你将字符串输入乘以10,20,5和50,所以如果你的所有输入都是1,你将得到一个字符串中的85 1。你需要的是在乘法之前将变量转换为整数:

q=10*int(raw_input("how many 10c coins do you have?"))
d=20*int(raw_input("how many 20c coins do you have?"))
n=5*int(raw_input("how many 5c coins do you have?"))
p=50*int(raw_input("how many 50c coins do you have?"))
tc=(q+d+n+p)

答案 5 :(得分:0)

到op,这是我写的代码......

quarterCount = float(raw_input("User, please enter the number of Quarters you have: "))
dimeCount = float(raw_input("User, please enter the number of Dimes you have: "))
nickleCount = float(raw_input("User, please enter the number of Nickles you have: "))
pennyCount = float(raw_input("User, please enter the number of Pennies you have: "))

然后我将值分配给下一个用于浮动硬币的变量,例如......

quarterCountProp = 0.25 * float(quarterCount)
print "You have $",
print "{0:.2f}".format(quarterCountProp),
print "worth of Quarters."

在节目结束时,为了得到我做的总计...

grandTotal = quarterCountProp + dimeCountProp + nickleCountProp + pennyCountProp
print "Your total pocket change is: $",
print "{0:.2f}".format(grandTotal)