-5
# 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循环。我希望用户可以在没有答案的情况下按 Enter 并跳出循环。在他爆发后,计算他之前添加的数字的总和,如果总和不是81。告诉用户他没有达到目标值,告诉他他达到了​​什么值并询问他是否要重新开始!

4

1 回答 1

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
于 2013-10-07T02:33:20.777 回答