我想将此 while 循环应用到下面的 for 循环中。我尝试在每个 if 语句中将 while 循环放在 if 语句之前。当我将它放在 if 语句之前(在 for 循环中)时,它会询问用户一次,然后为整个范围(1,8)返回相同的输入。我希望这个 while 循环适用于每个问题,即 2 到 8 的七个项目我如何实现它。谁能帮忙,谢谢
def valid_entry ():
price = 110
invalid_input = 1
while price< 0 or price> 100:
if invalid_input >=2:
print "This is an invalid entry"
print "Please enter a number between 0 and 100"
try:
price= int(raw_input("Please enter your price : "))
except ValueError:
price = -1
invalid_input +=1
while循环结束
def prices ():
x = range (1,8)
item=2
price=0
for item in x:
item +=1
print "\n\t\titem",item
price = int(raw_input("Enter your price : "))
if price <10:
price=1
print "This is ok"
if price >9 and price <45:
price +=5
print "This is great"
if price >44 and price <70:
price +=15
print "This is really great"
if price >69:
price +=40
print "This is more than i expected"
print "\nYou now have spent a total of ",price
prices ()
没有回应是否表明这是一个愚蠢的问题,还是不可能做到?
这是否使它更清楚?
def prices ():
x = range (1,8)
item=2
price=0
for item in x:
item +=1
print "\n\t\titem",item
valid_entry ()#should it go here
price = int(raw_input("Enter your price : "))
valid_entry ()#should it go here
if price <10:
valid_entry ()#should it go here and so on for the following 3 if conditions
price=1
print "This is ok"
if price >9 and price <45:
price +=5
print "This is great"
if price >44 and price <70:
price +=15
print "This is really great"
if price >69:
price +=40
print "This is more than i expected"
print "\nYou now have spent a total of ",price