我有一个学校项目用于制定博彩计划。它会询问帐户余额并检查用户是否可以玩更多游戏或告诉他们如果破产就停止。我不知道如何添加一个 while 循环来检查他们是否有足够的钱再次玩,并询问他们是否想再次玩。
代码如下:
import random
for x in range (1):
x=random.randint(1,31)
point=int(0)
savings = int(input("Enter your total amount of money you have in your bank account"))
betmoney = int(input("Enter the money you are betting"))
num = int(input("Enter a number between 1 and 30"))
bef = (savings - betmoney)
if num == x:
print ("you have guessed the mystery number well done. Your earnings just got multiplied by 6")
point = point + 6
import random
for x in range (1):
x=random.randint(1,31)
if num % 2 == 0:
print("since your number is even, you get double your money back")
point = point + 2
else:
point = point + 0
if num % 10 == 0:
print("since your number is a multiple of 10 you get 3x your money back")
point = point + 3
else:
point = point + 0
for i in range(2,num):
if (num % i) == 0:
point = point +0
break
else:
print(num,"is a prime number so you will get 5x your money back")
point = point + 5
if num < 5:
print("since your number is lower than 5 you get double your money back")
point = point + 2
else:
point = point + 0
else:
print("unfortunatley you have nor guessed the right mystery number. better luck next time.")
win = (point * betmoney)
aft = (bef + win)
print ("Your bank account after playing is now" , aft)
print ("You have won" , win)
print ("btw the mystery number was" , x)
请您参考如下方法:
我添加了一个 while 循环并想到了这个。
代码如下:
import random
point = int(0)
savings = int(input("Enter your total amount of money you have in your bank account : "))
play = bool(True)
while savings > 0 and play:
for x in range (1):
x = random.randint(0, 30)
betmoney = int(input("Enter the money you are betting : "))
if savings >= betmoney:
savings = savings - betmoney
else:
print("Please enter an amount less than ", savings)
continue
num = int(input("Enter a number between 1 and 30 : "))
if num == x:
print("you have guessed the mystery number well done. Your earnings just got multiplied by 6")
point = point + 6
import random
for x in range (1):
x = random.randint(1, 31)
if num % 2 == 0:
print("since your number is even, you get double your money back")
point = point + 2
else:
point = point + 0
if num % 10 == 0:
print("since your number is a multiple of 10 you get 3x your money back")
point = point + 3
else:
point = point + 0
for i in range(2,num):
if (num % i) == 0:
point = point +0
break
else:
print(num,"is a prime number so you will get 5x your money back")
point = point + 5
if num < 5:
print("since your number is lower than 5 you get double your money back")
point = point + 2
else:
point = point + 0
else:
print("unfortunatley you have nor guessed the right mystery number. better luck next time.")
win = (point * betmoney)
aft = (savings + win)
print("Your bank account after playing is now", aft)
print("You have won", win)
print("The mystery number was" , x)
if savings > 0:
play = bool(input("Do you want to play again (Y/N)? "))
print("You have no more money left. You are broke. Thanks for playing though!")
