Python Forum
Problem with an IF statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with an IF statement
#11
(Jan-30-2020, 02:16 PM)buran Wrote: How do you assign the value returned from input when you don't know what it will be?
I can write the code, but it will be more beneficial for you if you get it done yourself.

So, I think I have it.

"""
Random Number generator game
"""
import random

coin = ('heads','tails')
game = 0
while True:
        user_choice = input("Heads or Tails? ").lower()
        if user_choice in ("heads","h"):
            print("You have selected: {}".format(user_choice))
            user_choice = 'heads'
            break
        elif user_choice in ("tails","t"):
            print("You have selected: {}".format(user_choice))
            user_choice = 'tails'
            break
        else:
            print("You can only choose Heads or Tails")
            continue


coin_result = random.choice(coin)
print ("Coin has landed on: " ,coin_result)
if coin_result == user_choice:
        print("winner")
        game =+ 1
        print(game)
            
else:
        print("You lose")
Now I have to put the bottom section back in the while loop so that it can be replayed instead of ending.
I also need to somehow keep track of the games won as it tends to always just say "1" instead of accumulating it.

Is this what you meant?

Thank you!
Reply
#12
exactly
(Jan-30-2020, 02:26 PM)Ryan_Todd Wrote: I also need to somehow keep track of the games won as it tends to always just say "1" instead of accumulating it.
you swap the = and +, it should be game += 1

Now, when your code is functional, you can think how to improve it
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#13
(Jan-30-2020, 02:30 PM)buran Wrote: exactly
(Jan-30-2020, 02:26 PM)Ryan_Todd Wrote: I also need to somehow keep track of the games won as it tends to always just say "1" instead of accumulating it.
you swap the = and +, it should be game += 1

Now, when your code is functional, you can think how to improve it

Hi Buran,

I now have functional code and i am managing to make changes to make it better already! It now counts the games and how many you have won.

This is the code if you wondered:

"""
Random Number generator game
"""
import random

coin = ('heads','tails')
game_win = 0
game = 0
while True:
        user_choice = input("Heads or Tails? ").lower()
        if user_choice in ("heads","h"):
            print("You have selected: {}".format(user_choice))
            user_choice = 'heads'
            #break
        elif user_choice in ("tails","t"):
            print("You have selected: {}".format(user_choice))
            user_choice = 'tails'
            #break
        else:
            print("You can only choose Heads or Tails")
            continue

        game += 1
        coin_result = random.choice(coin)
        print ("Coin has landed on: " ,coin_result)
        if coin_result == user_choice:
            print("winner")
            game_win += 1
            print("You have won {} out of {} games".format(game_win,game))
            continue
            
        else:
            print("You lose")
Thanks for all your help, it was just what i needed :)
Reply
#14
(Jan-30-2020, 02:38 PM)Ryan_Todd Wrote: This is the code if you wondered:
Good work,but users should also have a way to quit out of the game.
Here with functions and a game_menu that fall back into when finish game.
From game_menu can start a new game or quit out.
Also all f-string,as it's better and also make code look better.
import random

def coin_game():
    coin = ('heads','tails')
    game_win = 0
    game = 0
    print('\nReturn to game menu press <Q>')
    while True:
        user_choice = input("Heads or Tails? ").lower()
        if user_choice == 'q':
            return
        if user_choice in ("heads", "h"):
            print("You have selected: {user_choice}")
            user_choice = 'heads'
        elif user_choice in ("tails", "t"):
            print("You have selected: {user_choice}")
            user_choice = 'tails'
        else:
            print("You can only choose Heads or Tails")
        game += 1
        coin_result = random.choice(coin)
        print(f"Coin has landed on: {coin_result}" )
        if coin_result == user_choice:
            print("winner")
            game_win += 1
            print(f"You have won {game_win} out of {game} games")
        else:
            print("You lose")

def game_menu():
    while True:
        print('(1) Play Head/Tail coin game')
        print('(2) Highscore')
        print('(Q) Quit')
        choice = input('Enter your choice: ').lower()
        if choice == 'q':
            return
        if choice == '1':
            coin_game()
        elif choice == '2':
            print('Not implemented yet\n')
            pass
        else:
            print(f'Not a correct choice: {choice}')

if __name__ == '__main__':
   game_menu()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with while statement. BobSmoss 3 1,670 Jan-08-2022, 03:22 PM
Last Post: BobSmoss
  Problem in if-else statement shantanu97 2 2,438 Apr-09-2021, 06:37 AM
Last Post: shantanu97
  multiple condition if statement problem FelixReiter 3 2,600 Jan-11-2021, 08:07 AM
Last Post: FelixReiter
  Problem with If statement and dataframe Milfredo 1 1,774 Sep-16-2020, 05:50 AM
Last Post: Milfredo
  Problem with If else statement Milfredo 5 2,592 Aug-30-2020, 06:32 PM
Last Post: Milfredo
  Problem with a 'break' statement. christopher3786 3 2,450 Jun-20-2020, 10:16 AM
Last Post: pyzyx3qwerty
  Problem with 'and' in 'if' statement CoderMan 3 2,537 Oct-06-2019, 07:32 PM
Last Post: buran
  Why doesn't my loop work correctly? (problem with a break statement) steckinreinhart619 2 3,211 Jun-11-2019, 10:02 AM
Last Post: steckinreinhart619
  Problem with elif statement Haddal99 2 2,278 May-20-2019, 09:26 AM
Last Post: avorane
  if statement and in operator problem bobger 5 4,033 Nov-30-2017, 06:50 PM
Last Post: bobger

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020