Python3.2循环

时间:2013-10-07 01:39:03

标签: python loops while-loop

# generate a random number between 1 and 99 sgenrand.randint(1,99) # your code goes here 
print("Enter coins that add up to 81 cents, one per line.") 
#promp the user to start entering coin values that add up to 81  
coin = (sgenrand.randint(1,99)) 
number1 = ("Enter first coin: ") 
sum = 0   
number1 = eval(input("Enter first coin: ")) 
while number1 != coin:   
    if number1 != coin: 
    number1 = eval(input("Enter next coin: "))
我停留在这个while循环中。我希望用户可以在没有答案的情况下点击输入并突破循环。在他爆发之后,计算他之前添加的数字总和,如果总和不是81.告诉用户他没有达到目标值,告诉他他达到了​​什么值,并询问他是否想重新开始!

1 个答案:

答案 0 :(得分:0)

我喜欢做作业,我真的这么做:

import random as sgenrand

def oneRound():
    target = sgenrand.randint(1, 99)
    print('Enter coins that add up to {} cents, one per line.'.format(target))
    total = int(input('Enter first coin: '))
    while True:
        s = input('Enter next coin: ')
        if not s: break
        total += int(s)
    if total == target:
        print('Well done.')
        return True
    print('You reached {}.'.format(total))
    return input('Do you want to start over? [y/*] ') != 'y'

while not oneRound(): pass