错误消息:TypeError:不支持的操作数类型 - :' str'并且'漂浮'

时间:2014-10-22 17:07:06

标签: python python-2.7 typeerror

我正在为我的GCSE课程编写一个程序,就像自动售货机一样。我试图减去一个重写变量的变量,由于某种原因它只是没有用。帮助

This is my code:
credit=raw_input("Please input your change, CAREFUL! This Machine only accepts 10p,20p,50p and £1: ")
list1= ["1", "2", "3", "4", "5"]
price1=0.65
price2=0.70
price3=0.50
price4=0.70
price5=0.80
while (credit) == 0:
   (credit)=raw_input("Please input your change: ")
products = (" 1) Kinder Bueno\n 2) Doritos Chilli Heatwave\n 3) Nestle Yorkie Bar\n 4) Coca Cola(Can)\n 5) Volvic Stawberry Water")
print (credit)
print ("The product selection is the following")
print (products)
(selection)=raw_input("Please select a product: ")
if (selection) == (list1[0]):
   (new_credit)=int(credit-price1)
   print (new_credit)

1 个答案:

答案 0 :(得分:1)

在python中

你的所有( )都是多余的,我相信你的真正含义。

credit = raw_input("Please input your change, CAREFUL! This Machine only accepts 10p,20p,50p and £1: ")
list1= ["1", "2", "3", "4", "5"]
price1 = 0.65
price2 = 0.70
price3 = 0.50
price4 = 0.70
price5 = 0.80
while credit == 0:
   credit = raw_input("Please input your change: ")
products = " 1) Kinder Bueno\n 2) Doritos Chilli Heatwave\n 3) Nestle Yorkie Bar\n 4) Coca Cola(Can)\n 5) Volvic Stawberry Water"
print credit
print "The product selection is the following"
print products
selection = raw_input("Please select a product: ")
if selection == list1[0]:
   new_credit = int(credit) - price1  # NOTE THE CHANGE HERE :)
   print new_credit

注意上面的更改(除了删除冗余的parens)。演员应该只是信用,而不是信用没有价格。

注意

为了与Python3兼容,将parens放入print语句(因为print现在是一个函数)可能是值得的,但是对于控制流语句,例如if / else / while,它们绝对不需要它们。 .etc