Python机会游戏

时间:2019-03-09 17:47:03

标签: python python-3.x

我有一个学校项目用于制定投注程序。它要求提供帐户余额并检查用户是否可以玩更多游戏,或者如果用户破产了则告诉他们停止游戏。我不知道如何添加一个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)

2 个答案:

答案 0 :(得分:1)

我添加了一个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!")

答案 1 :(得分:0)

如果使用的是if语句,则可以通过为您需要玩的钱数设置一个停止点来尝试某些操作(例如,您必须有$ 1,000的钱才能玩)。 现在,让我们假设您需要玩$ 1000,然后在您已经存在的变量aft中调用它。代码如下:

while aft > 1,000:
    #confirms that the user can play
    print("You can play! Have fun!")
    #(Enter your current code here)
     if aft <1,000:
         print("Sorry, but your current value of " + aft + "is not enough to play. Please earn more money.")
         exit()