简体   繁体   中英

Python: How do I call upon a variable in my code from a user input?

I have a list of teams that each have a score attached to them and I would like to compare the teams scores just by the user in putting the team names for example two teams could be

BOS = 55

PHI = 52

I first prompt the user to choose the home team but I want them to type out the team say P,H,I. Then I get them to type out the visiting team B,O,S. Then the program would tell you to choose BOS to win. I just don't know what input command to use so that the user has to type out the letters of the team and that goes to that teams respective score that goes to the if statement to decide who wins.

Summed up I am having trouble with the input part.

Maybe something like this?

home_teams = {'team1': 50, 'team2': 60, 'team3': 70, 'team4': 80}
visiting_teams = {'team5': 55, 'team6': 45, 'team7': 30, 'team8': 90}

_hteam = input('Plase select a home team: ')
if _hteam not in list(home_teams.keys()):
    print(f'\nInvalid team selected. Please select form {list(home_teams.keys())}')
else:
    _vteam = input('\nPlease select a visiting team: ')
    if _vteam not in list(visiting_teams.keys()):
        print(f'\nInvalid team selected. Please select from {list(visiting_teams.keys())}')
    else:
        if home_teams[_hteam] > visiting_teams[_vteam]:
            print(f'\nSelect the {_hteam} to win.')
        elif home_teams[_hteam] < visiting_teams[_vteam]:
            print(f'\nSelect the {_vteam} to win.')

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