简体   繁体   中英

I need help to restart a game in pyhton

I just don't know how to make a loop and thank you for your help.

from random import randint
print ('Bem vindo!')
random = randint(1, 100)
escolha = 0
reiniciar = 0

while escolha!= random:
     escolha = int(input ('Escolha entre 1 e 100: '))

     if escolha == random:
          print ('Venceste!')
     else:
          if escolha > random:
             print ('Alto')
          else:
             print ('Baixo')
print ('Fim do jogo!')

I dont understand the languange but i think you need to change your logic abit:


print ('Bem vindo!')

random = randint(1, 100)

escolha = 0

reiniciar = 0

while escolha!= random:

    escolha = int(input ('Escolha entre 1 e 100: '))

    if escolha == random:

        print ('Venceste!')

    elif escolha > random:
        print ('Alto')
    else:
        print ('Baixo')
print ('Fim do jogo!')```

Welcome to StackOverflow.

So you want to create a CLI to your game right? Try to play with while and input functions, and always try to think as the user. Sometimes he doesn't act as we expect.

from random import randint

print ('Bem vindo!')

jogar = 'a'

while jogar not in 'nN':
    random = randint(1, 100)
    escolha = 0
    reiniciar = 0


    while escolha!= random:

        escolha = int(input ('Escolha entre 1 e 100: '))

        if escolha == random:
            print ('Venceste!')
        elif escolha > random:
            print ('Alto')
        else:
            print ('Baixo')

    jogar = input('Deseja jogar um novo jogo? (y/n)\n')
    while jogar not in 'nNyY':
        jogar = input('Escolha uma opção válida (y/n)\n')

print ('Fim do jogo!')

Saudações Brasileiras!!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM