Python Forum
Code giving same output no matter the input. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Code giving same output no matter the input. (/thread-31568.html)



Code giving same output no matter the input. - Yort - Dec-20-2020

I'm very new to programming and am trying to create a coin flipping program in which you call heads or tails, it simulates a flip, and then tells you whether you were right or wrong. My program is working fine until it comes to giving the player their result ("Win" or "loss") as it always outputs "loss". If anyone could help that would be great.

import random

print("Heads[1] or Tails[2]?\n")
call = input()

flip = random.randint(1, 2)

if flip == 1:
    print("\nIt landed on: Heads")
else:
    print("\nIt landed on: Tails")

if flip == call:
    print("\n[YOU WIN]")
else:
    print("\n[YOU LOSE]")



RE: Code giving same output no matter the input. - deanhystad - Dec-20-2020

flip is a number. call is a string. They will never be equal. You have to either change flip to a string or call to a number (int).

I don't like using meaningless numbers, so I make my coin have Heads and Tails instead of 1 and 2.
import random

coin = ('Heads', 'Tails')
call = input(f'Call it {coin}: ')
flip = random.choice(coin)
 
if flip[0] == call[0].upper():
    result = 'YOU WIN'
else:
    result = 'YOU LOSE' 
print(f'\nIt landed on {flip}. {result}')



RE: Code giving same output no matter the input. - buran - Dec-20-2020

The problem with your code is that input will return str, while randint will yield int. You are comparing e.g. '1' with 1. Just convert the value returned from input() into int.

As a side note, input() takes an argument - the prompt to display. So, instead of 2 lines you can have call = input("Heads[1] or Tails[2]? ")