Home > Software engineering >  I was using Python and If statement won't write ( < ) and keep writing ( > ) whether they
I was using Python and If statement won't write ( < ) and keep writing ( > ) whether they

Time:11-24

print("Welcome to Agurds. Before we begin can you tell me your name?")
Name = input("name: ")
print("Hello "   Name   " When were you born "   Name   "?")
year = int(input("Born year:"))
age = str(2022 - year)
print("You must be "   age   " this year.")
if age < str(18):
    print("You're too young to be here. Exiting world.")
else:
    print("I see we have an adult here. Would you like to buy some of our products before hand?")
    Pens = input("How much do you have right now?")
    if Pens < str(100):
        print("You can only buy some of our products?")
    if Pens > str(100):
        print("You can buy most of our products")

The result: Welcome to Agurds. Before we begin can you tell me your name? name: Hein Hello Hein When were you born Hein? Born year:2000 You must be 22 this year. I see we have an adult here. Would you like to buy some of our products before hand? How much do you have right now? 99 You can buy most of our products

I was expecting it to give me the first line I wrote but it doesn't work. I'm just started python not too long ago so I don't know what I am doing wrong.

CodePudding user response:

When comparing two values to find the smallest, use int rather than str, such as using if age < 18 rather than if age < str(18). You problem was that it was comparing the string Pens rather than the int Pens

print("Welcome to Agurds. Before we begin can you tell me your name?")
Name = input("name: ")
print("Hello "   Name   " When were you born "   Name   "?")
year = int(input("Born year:"))
age = 2022 - year
print("You must be "   str(age)   " this year.")
if age < 18:
    print("You're too young to be here. Exiting world.")
else:
    print("I see we have an adult here. Would you like to buy some of our products before hand?")
    Pens = int(input("How much do you have right now?"))
    if Pens < 100:
        print("You can only buy some of our products?")
    if Pens > 100:
        print("You can buy most of our products")

Hope this helps

CodePudding user response:

You are trying to compare strings. Saying "95" < "100" doesn't make much sense. What you could do, is convert the pens input to a int, and then compare it to 100:

pens = int(input("How much do you have right now?"))
if pens < 100:
    #your code

Also, avoid using capitalizer names for variables, since most programming languages will detect that as an error

  • Related