编写一个Python程序,重复要求用户输入硬币值,直到 总金额与目标价值相匹配。目标值是随机的 生成1到99之间的整数(含)。
例如,对于目标值31,用户输入的硬币值可以 是25,5和1因为31 = 25 + 5 + 1。
为了在Python中为目标值生成随机数,您必须这样做 在程序开头写下以下语句:
import random
然后,您可以在程序中编写以下语句以生成随机语句 号码:
rand = random.randint(1, 99)
上述语句将生成1到99之间的随机数(两者都是 终点是包容性的)。该随机数将存储在变量rand中 (根据上述声明)。如果需要,您可以使用其他变量名称。 存储在名为rand的变量中的随机值是目标硬币值 用户必须输入硬币。
程序的示例运行
在下一页是程序的完整示例运行。这是个 程序如何表现的例子;在下文中,59,70和76 是随机生成的数字。
实施例: 此练习的目的是输入多个硬币值 加起来显示的目标值。
输入1-penny,5-nickel,10-dime和25-quarter的硬币值。 在最后输入的硬币值后点击返回。
Enter coins that add up to 59 cents, one per line.
Enter first coin:
Enter first coin: 3
Invalid entry
Enter first coin: 25
Enter next coin: 5
Enter next coin: 25
Enter next coin: 2
Invalid entry
Enter next coin: 1
Enter next coin: 1
Enter next coin: 1
Enter next coin: 1
Enter next coin:
Correct!
Try again (y/n)?: y
Enter coins that add up to 70 cents, one per line.
Enter first coin: 2
Invalid entry
Enter first coin: 25
Enter next coin: 25
Enter next coin: 10
Enter next coin: 5
Enter next coin: 1
Enter next coin: 25
Sorry - total amount exceeds 70 cents.
Try again (y/n)?: y
Enter coins that add up to 76 cents, one per line.
Enter first coin: 25
Enter next coin: 25
Enter next coin: 1
Enter next coin: 25
Enter next coin:
Correct!
Try again (y/n)?: n
Thanks for playing ... goodbye
这是我到目前为止的代码:
import random
def chkcoin(acoin):
basecoin = ["1"," 5", "10", "25"]
flag = False
for bc in basecoin:
if acoin == int(bc):
return True
else:
flag = True
if flag:
print('Invalid entry')
return False
def tryAgain():
comd = input('Try again (y/n)?: ')
if comd == 'y':
return True
elif comd == 'n':
print ('Thanks for playing ... goodbye')
return False
else:
print ('Command error! Please enter y or n.')
print ('Thanks for playing ... goodbye')
return False
导入随机
def chkcoin(acoin): basecoin = ["1"," 5", "10", "25"] flag = False for bc in basecoin: if acoin == int(bc): return True else: flag = True if flag: print('Invalid entry') return False def tryAgain(): comd = input('Try again (y/n)?: ') if comd == 'y': return True elif comd == 'n': print ('Thanks for playing ... goodbye') return False else: print ('Command error! Please enter y or n.') print ('Thanks for playing ... goodbye') return False
有人能帮助我吗?我有2个问题需要解决。
当我需要输入号码时。如果我输入空格键。这个程序会崩溃。我想如果我输入空格键,结果将像8或其他。 "无效的尝试"
这个python会在一场比赛后崩溃。怎么解决?我在python2中编写这个python。突然间,我发现我需要在python3中编写。但我不知道如何改变它。 TY
答案 0 :(得分:1)
您想要查看while循环
例如:
# Set the target value
target = 50
# Initialize the running total to 0
total = 0
run the indented code while target != total
while total != target:
# ask the user for a number
choice = input("Number? ")
# add choice to total
total += choice
以上while
会在total != 50
评估为True
的情况下继续运行。
答案 1 :(得分:0)
import random
def amountGame():
coin=[25,5,1]
target=random.randint(1,99)
print(f'Your Amount is {target}')
total=0
while target!=total:
x=int(input('Enter pic of amount :'))
if x in coin and (target-total)>=x:
total+=x
else:
print('Envalid Entry')
print('You Solve problam')
opt=''
while opt!='N':
amountGame()
opt=input('Do you want to continue press Y else N :).upper()