为我的小游戏创建循环

时间:2017-06-06 01:52:26

标签: python

我正在用Python制作一个简单的游戏。我正在努力制作游戏循环。有人可以帮助我完成我的小游戏。

import random
print ('Welcome to the Lottery')
dollar = 100
print 'Your money: $' + str(dollar)
print 'Enter your bet:'
bet= raw_input('(ex.$100)')

remaining = dollar - int(bet)
if bet <= dollar:
    print 'Your money: $' + str(remaining)
    print 'Bet:$' + bet
elif bet > dollar:
    print 'You don\'t have enough money'
    raw_input('(ex.$100)')
    while bet > dollar:
        print 'You don\'t have enough money'
        raw_input('(ex.$100)')
else:
    print 'You don\'t have any bets.'

raw_input('Enter you number (1-999):')
raw_input('Play? (yes or no)')
for i in range(1):
        (random.randint(1,999))
        print(random.randint(1,999))

1 个答案:

答案 0 :(得分:0)

您可以使用while作为循环。

例如,如果您想继续重复您的程序,直到用户键入特定单词(例如“退出”):

import random
print ('Welcome to the Lottery')
dollar = 100
bet = ""
while not bet == "exit":
    print 'Your money: $' + str(dollar)
    print 'Enter your bet:'
    bet= raw_input('(ex.$100)')

    remaining = dollar - int(bet)
    if bet <= dollar:
        print 'Your money: $' + str(remaining)
        print 'Bet:$' + bet
    elif bet > 0:
        print 'You don\'t have enough money'
    else:
        print 'You don\'t have any bets.'


    for i in range(1):
            (random.randint(1,999))
            #return(random.randint(1,999))
print("Goodbye!") # Prints 'Goodbye!' after the user types 'exit'.

或者,您可以使用while True进行无限循环。

以下是一些信息和示例:http://www.tutorialspoint.com/python/python_while_loop.htm