im creating a adventure game in python .
name = input('what is your name : ')
print(f'welcome {name} to this game ! ')
choice = input('do you want to play the game ? ')
if choice.lower() == 'y':
print('ok , cool ! ')
else :
print('bye')
advent = input('there is a road . do you want to go left or right ? ')
if advent.lower() == 'left':
path = input('you have reached an river . do you want to swim or cross a bridge ? ')
if path.lower() == 'bridge':
print('you have crossed the bridge congrats ! ')
if path.lower() == 'swim':
croc = input('now you are swimming . oops ! there is a crocodile . what do you want to do ? ')
if croc.lower() == 'go back to the river bank':
bank_path = input('now you are in the river bank and do you want to cross the bridge ? ')
if bank_path == 'cross the bridge':
# how do i go back to the if statement in line 12
if croc.lower() == 'go ahead':
print('you are ded -_- ! . The croc ate you ')
now in line 16 , if the user wants to go back to the river bank , there is only one choice which is crossing the river . now how do i make the if statement which is line 12 to work in line 19
CodePudding user response:
To reuse code in python, functions are the way to go:
def crossed_bridge():
print('you have crossed the bridge congrats ! ')
However in your case, the code you want to reuse is so short that you can just call print()
another time.