Python Forum
Code giving same output no matter the input.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code giving same output no matter the input.
#1
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]")
Reply
#2
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}')
Reply
#3
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]? ")
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


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in output of a snippet code akbarza 2 375 Feb-28-2024, 07:15 PM
Last Post: deanhystad
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,175 Sep-24-2023, 05:03 AM
Last Post: deanhystad
Question in this code, I input Key_word, it can not find although all data was exact Help me! duchien04x4 3 1,050 Aug-31-2023, 05:36 PM
Last Post: deanhystad
  output provide the filename along with the input file processed. arjunaram 1 943 Apr-13-2023, 08:15 PM
Last Post: menator01
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,568 Mar-27-2023, 07:38 AM
Last Post: buran
  I cannot able to see output of this code ted 1 756 Feb-22-2023, 09:43 PM
Last Post: deanhystad
  Code won't break While loop or go back to the input? MrKnd94 2 961 Oct-26-2022, 10:10 AM
Last Post: Larz60+
  Help Switching between keyboard/Mic input in my code Extra 1 1,098 Aug-28-2022, 10:16 PM
Last Post: deanhystad
  why I dont get any output from this code William369 2 1,131 Jun-23-2022, 09:18 PM
Last Post: William369
  How can I organize my code according to output that I want ilknurg 1 1,180 Mar-11-2022, 09:24 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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