我想将while循环应用到下面的for循环中。 我已经尝试在每个if语句中将if循环放在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
答案 0 :(得分:1)
你可以尝试这样的事情(如果这不是你想要的,那就道歉)。很高兴解释任何没有意义的事情 - 总的想法是它循环通过8个项目的范围,每次要求有效价格并继续询问输入的值是否不是指定范围内的数字。由于这可能适用于作业,我试图将其与您已经证明的已知的概念保持一致(这里唯一的例外可能是continue
):
def valid_entry():
# Here we define a number of attempts (which is what I think
# you were doing with invalid_input). If the person enters 10
# invalid attempts, the return value is None. We then check to
# see if the value we get back from our function is None, and if
# not, proceed as expected.
num_attempts = 0
while num_attempts < 10:
# Here we do the input piece. Note that if we hit a ValueError,
# the 'continue' statement skips the rest of the code and goes
# back to the beginning of the while loop, which will prompt
# again for the price.
try:
price = int(raw_input("Enter your price : "))
except ValueError:
print 'Please enter a number.'
num_attempts += 1
continue
# Now check the price, and if it isn't in our range, increment
# our attempts and go back to the beginning of the loop.
if price < 0 or price > 100:
print "This is an invalid entry"
print "Please enter a number between 0 and 100"
num_attempts += 1
else:
# If we get here, we know that the price is both a number
# and within our target range. Therefore we can safely return
# this number.
return price
# If we get here, we have exhausted our number of attempts and we will
# therefore return 'None' (and alert the user this is happening).
print 'Too many invalid attempts. Moving to the next item.'
return None
def prices():
# Here we go from 1-8 (since the upper bound is not included when
# using range).
x = range(1,9)
# Here we use a variable (total) to store our running total, which will
# then be presented at the end.
total = 0
# Now we iterate through our range.
for item in x:
print "\n\t\titem",item
# Here is the important part - we call our valid_entry function and
# store the result in the 'price' variable. If the price is not
# None (which as we saw is the return value if the number of attempts
# is > 10), we proceed as usual.
price = valid_entry()
if price is not None:
if price <10:
# Note this should probably be += 1
total += 1
print "This is ok"
elif price >= 10 and price < 45:
total += 5
print "This is great"
elif price >= 45 and price < 70:
total += 15
print "This is really great"
# We know that price >= 70 here
else:
total += 40
print "This is more than i expected"
# Now print out the total amount spent
print "\nYou now have spent a total of ", total
prices()