Python While While语句大于/小于符号

时间:2016-01-29 18:08:28

标签: python syntax-error

print ("Hello, this is a change return program, coded by A person")
cost = float(input("How much did the object cose (in £s, eg 0.50 for 50p)?"))
given = float(input("How much money did you pay (in the same format)?"))
change = given - cost
twentypounds = 0
tenpounds = 0
fivepounds = 0
twopounds = 0
onepound = 0
fiftypennies = 0
twentypennies = 0
tenpennies = 0
fivepennies = 0
twopennies = 0
onepenny = 0
print(change)
while change >= 20:
    change = change - 20
    twentypounds + 1
print(twentypounds)

while change >=10 and <20 :
    change = change - 10
    tenpounds + 1
print(tenpounds)

while change 10> and >= 5:
    change = change - 5
    fivepounds + 1
print(fivepounds)

while change <5 and >= 2:
    change = change - 2
    twopounds + 1
print(twopounds)

while change <2 and >= 1:
    change = change - 1
    onepound + 1
print(onepound)

while change <1 and >= 0.5:
    change = change - 0.5
    fiftypennies + 1
print(fiftypennies)

while change <0.5 and >= 0.2:
    change = change - 0.2
    twentypennies + 1
print(twentypennies)

while change <0.2 and >= 0.1:
    change = change - 0.1
    tenpennies + 1
print(tenpennies)

while change  <0.1 and >= 0.05:
    change = change - 0.05
    fivepennies + 1
print(fivepennies)

while change <0.05 and >= 0.02:
    change = change - 0.02
    twopennies + 1
print(twopennies)

while change <0.02 and >= 0.01:
    change = change - 0.01
    onepennies + 1
print(onepenny)


print("The Change Value is now:", change, "  This is for debugging only")
twenty_pounds = twentypounds * 20
ten_pounds = tenpounds * 10
five_pounds = fivepounds * 5
two_pounds = twopounds * 2
one_pound = onepound * 1
fifty_pennies = (fiftypennies * 50) / 100
twenty_pennies = (twentypennies * 20) / 100
ten_pennies = (tenpennies * 10) / 100
five_pennies = (fivepennies * 5) / 100
two_pennies = (twopennies * 2) / 100
one_penny = (onepenny * 1) / 100
total_change = twenty_pounds + ten_pounds + five_pounds + two_pounds + one_pound + fifty_pennies + twenty_pennies + ten_pennies + five_pennies + two_pennies + one_penny
print("Your Total Change is:", total_change)

当它确实工作时,它只返回0.0次,现在一直给我无效的语法,我试图交换标志,然后还有数字,但是没有对任何事情有效,任何想法如何解决这个问题?

新代码:(仍然没有工作,虽然现在没有语法错误,但只返回0(尽管数字在第4行有所变化)

print ("Hello, this is a change return program, coded by A person")
cost = float(input("How much did the object cose (in £s, eg 0.50 for 50p)?"))
given = float(input("How much money did you pay (in the same format)?"))
change = given - cost
twentypounds = 0
tenpounds = 0
fivepounds = 0
twopounds = 0
onepound = 0
fiftypennies = 0
twentypennies = 0
tenpennies = 0
fivepennies = 0
twopennies = 0
onepenny = 0
print(change)
while change >= 20:
    change = change - 20
    twentypounds + 1
print(twentypounds)

while change >= 10 and change < 20:
    change = change - 10
    tenpounds + 1
print(tenpounds)

while change > 10 and change >= 5:
    change = change - 5
    fivepounds + 1
print(fivepounds)

while change < 5 and change >= 2:
    change = change - 2
    twopounds + 1
print(twopounds)

while change < 2 and change >= 1:
    change = change - 1
    onepound + 1
print(onepound)

while change < 1 and change >= 0.5:
    change = change - 0.5
    fiftypennies + 1
print(fiftypennies)

while change < 0.5 and change >= 0.2:
    change = change - 0.2
    twentypennies + 1
print(twentypennies)

while change < 0.2 and change >= 0.1:
    change = change - 0.1
    tenpennies + 1
print(tenpennies)

while change  < 0.1 and change >= 0.05:
    change = change - 0.05
    fivepennies + 1
print(fivepennies)

while change < 0.05 and change >= 0.02:
    change = change - 0.02
    twopennies + 1
print(twopennies)

while change < 0.02 and change >= 0.01:
    change = change - 0.01
    onepennies + 1
print(onepenny)


print("The Change Value is now:", change, "  This is for debugging only")
twenty_pounds = twentypounds * 20
ten_pounds = tenpounds * 10
five_pounds = fivepounds * 5
two_pounds = twopounds * 2
one_pound = onepound * 1
fifty_pennies = (fiftypennies * 50) / 100
twenty_pennies = (twentypennies * 20) / 100
ten_pennies = (tenpennies * 10) / 100
five_pennies = (fivepennies * 5) / 100
two_pennies = (twopennies * 2) / 100
one_penny = (onepenny * 1) / 100
total_change = twenty_pounds + ten_pounds + five_pounds + two_pounds + one_pound + fifty_pennies + twenty_pennies + ten_pennies + five_pennies + two_pennies + one_penny
print("Your Total Change is:", total_change)

现在代码打印: (改变是正确的) 0.0,0.0,0.0等......

代码现在有效,感谢您的回答!

2 个答案:

答案 0 :(得分:5)

您的问题是change >=10 and <20。这没有意义,因为它没有指定应与20进行比较的内容。如果将表达式完全写为change >= 10 and change < 20,则代码应该起作用。或者,这可以更简洁地写成10 <= change < 20

答案 1 :(得分:1)

==是一个比较 =是作业

您在应该使用的地方使用== =