0

I was trying to play around with the input function. However, it seems that this code is not correct, I looked to see examples online but they tend to be basic and what i was trying to do was slightly above basic. Also, is it possible to put the return function instead of the print one? if not why? I'm new to this so if these all sound stupid please forgive me.

cheers!

def user_data(x):   
    age = input("how old are you?")
    if age == 20:
        print("you win")
    else:
        print("you lose")
azro
  • 53,056
  • 7
  • 34
  • 70
Kovacic95
  • 91
  • 5

2 Answers2

1

input returns a string, while you are comparing age (a string) to an integer.

You have to either compare age to a string (so age == "20"), or convert age to an int (so int(age) == 20).

See docs to find out how input works.

hmhmmm
  • 333
  • 1
  • 4
0
def user_data(age):
    if age == 20:
        print("you win")
    else:
        print("you lose")

age = int(input("how old are you?")) 
user_data(age)
Apeal Tiwari
  • 154
  • 2
  • 12
  • line 2: change `age` to `x` if you want it to use the parameter you pass in. – James Whiteley May 21 '20 at 18:50
  • This gives me "you win" all the time regardless of which number i put – Kovacic95 May 21 '20 at 19:10
  • Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel May 22 '20 at 11:44