Can anyone help I am a beginner? I want the variable choice to be displayed after the menu is but I need the variable to be on top so the name inserted can be shown beside add player name.
choice = input("Input your menu choice: ")
choice = False
if choice == "1":
name = input("What is your name? ")
print(" Menu ")
print("------------------------------------------")
print(f"[1] Add player name: {name} ")
print("[2] Play guess the capital city")
print("[3] End game")
print("------------------------------------------")
choice True
I tried to use a Boolean but it didn't so any help would be great.
CodePudding user response:
How about defining a string first like this?
import random
name = 'Anonomous'
playing = True
while playing == True:
print(" Menu ")
print("------------------------------------------")
print(f"[1] Add player name: {name} ")
print("[2] Play guess the capital city")
print("[3] End game")
print("------------------------------------------")
choice = input("Input your menu choice: ")
if choice == "1":
name = input("What is your name? ")
if choice == "2":
winner = False
capital_city = random.choice(['London', 'Paris', 'Rome'])
while not winner:
guess = input("What capital city am I thinking of? ").title()
if guess == capital_city:
print(f'You won!!! I was thinking of {guess}..')
winner = True
else:
print(f'No, it was not {guess}, guess again..')
if choice == "3":
exit()
CodePudding user response:
This part:
choice = input("Input your menu choice: ")
choice = False
will make choice always equal False, also you should avoid changing types of variables like above: from str to bool. Assuming that you want working and well-structured console game:
name = 'Unnamed' # Set default value of name.
# Create game loop using infinite while.
while True:
# Display the menu: use string multiplication and a little of math.
print(' ' * 18 'Menu' ' ' * 18)
print('-' * 40)
print(f"[1] Add player name: {name} ")
print("[2] Play guess the capital city")
print("[3] End game")
print('-' * 40)
# Get choice from user.
choice = input("\nInput your menu choice: ")
# Perform proper action for a choice selected.
if choice == '1':
name = input("What is your name? ")
elif choice == '2':
# Load your game ...
...
# If player wants to end game, it is equivalent to exiting
# loop using 'break' statement.
elif choice == '3':
break
else:
# Define what happens if player inputted unsupported choice.
...
# It is a common way to clear screen in python console
# applications/games, if fourty lines of empty lines is not
# sufficient increase amount of them.
print('\n' * 40)
# Here you can save for instance player score or nickname and
# later read it at the beginning of file (before the game loop).