Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code error
#1
Hello Friends

I am new to Python world. I am trying to practice some code and I am not getting the result as expected.

Here is the code:

x = input ("enter value of x")
y = input ("enter value of Y")
if (x > y):
    print("the value of x is " + x)
else:
    print("the value of y is  y" + y)
can you please guide me whether this is correct? Correct me if I am wrong.

Thanks you all. Your help is highly appreciated
Reply
#2
I'm guessing you are entering numbers in for x & y and then wondering why the comparisons don't work correctly.
input returns a string, not a number, it would need converting to an int or a float depending on what you require
x = int(x)
or
x = float(x)

For printing variables, you can use what's called an f string
by placing an f in front of the quotes, variables can be placed inside of {} and will be converted to there string value
print(f"the value of x is {x}")
Reply
#3
Thanks lot my friend. It works.

x = 10
y = 50
if (x > y):
    print(f"the value of x is {x}")
else:
    print(f"the value of y is  {y}")
Reply


Forum Jump:

User Panel Messages

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