Cómo bucle de nuevo al principio

votos
0

¿Cómo puedo solucionar este problema:

print 'Adventure Game'

choice_1 = raw_input('You are travelling down a long road, there is a fork in the route, one side goes to a jungle, and the other leads to a hill biome, which side do you choose?            J for Jungle, H for Hill ')

if choice_1 == 'J':
    print 'Jungle?, very well then' 

elif choice_1 == 'H':
    print 'Hill, good decision'

if choice_1 == 'J':
    choice_2 = raw_input('In the jungle, a king cobra appears behind you, as you look forward, an ape leaps from a tree onto the ground. Do you take your chances with the venomous king cobra, or the enormous ape? C for cobra, A for ape ')

while choice_2 != 'A':
    print 'Sorry, you were bit by the cobra and died'
    print 'Please try again'

if choice_2 == 'A':
    break

elif choice_2 == 'A':
    print 'You were almost mauled by the ape, luckily, it fleed the scene after loosing sight of you'

yo quiero que se reinicie, si el usuario elige c para choice_2, y para que se inicie desde el principio cada vez que esto sucede

Publicado el 09/10/2019 a las 18:56
fuente por usuario
En otros idiomas...                            


3 respuestas

votos
1

Se recomienda usar las funciones para cada lugar, de esa manera se puede llamar a la misma función varias veces o dentro de otras funciones.

Por ejemplo:

def road():
    choice = ''
    while choice not in ['j','h']:
        choice = raw_input('You are travelling down a long road, there is a fork in the route, one side goes to a jungle, and the other leads to a hill biome, which side do you choose?            J for Jungle, H for Hill ').lower()
    if choice == 'j':
        jungle()
    elif choice == 'h':
        hill()

def jungle():
    choice = ''
    while choice not in ['c','a']:
        choice = raw_input('In the jungle, a king cobra appears behind you, as you look forward, an ape leaps from a tree onto the ground. Do you take your chances with the venomous king cobra, or the enormous ape? C for cobra, A for ape ').lower()

    if choice == 'c':
        print 'Sorry, you were bit by the cobra and died'
        print 'Please try again'
        return
    elif choice == 'a':
        print 'You were almost mauled by the ape, luckily, it fleed the scene after loosing sight of you'
        next_func_here()

while True:
    print 'Adventure Game Starts Here!'
    road()
    print 'restarting...'

De esta manera puede seguir añadiendo funciones de forma rápida y sencilla, y es también más fácil de depurar. El whilebucle significa que si road()las salidas, se realiza un bucle de vuelta al principio y se reinicia road().

Respondida el 09/10/2019 a las 19:23
fuente por usuario

votos
0
print 'Adventure Game'
choice_1 = raw_input('You are travelling down a long road, there is a fork in the route, one side goes to a jungle, and the other leads to a hill biome, which side do you choose?            J for Jungle, H for Hill ')
if choice_c == 'J':
    while True:
        choice_2 = raw_input('In the jungle, a king cobra appears behind you, as you look forward, an ape leaps from a tree onto the ground. Do you take your chances with the venomous king cobra, or the enormous ape? C for cobra, A for ape ')
        if choice_2 != 'A':
            print 'Sorry, you were bit by the cobra and died'
            print 'Please try again'
        elif choice_2 == 'A':
            print 'You were almost mauled by the ape, luckily, it fleed the scene after loosing sight of you'
            break
elif choice_1 == 'H':
    print 'Hill, good decision'
Respondida el 09/10/2019 a las 19:21
fuente por usuario

votos
-1

se puede hacer una entrada en la parte inferior que dice "le gustaría continuar?" y ponerlo a una variable. Luego envolver todo su juego en un bucle while.

cont = True
while continue == True:
    #put all your code here
    cont = input("Would you like to continue? (T/F)")
Respondida el 09/10/2019 a las 19:07
fuente por usuario

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more