3

So I am learning python and there is something that bothers me. For the chunk of sample code below:

print("Test")

while True:
    print("statement0")

    x = input()
    print(x)

    if x == 1:
        print("statement1")
        y += input()
        print(y)

    elif x == 2:
        print("statement2")
        y += input()
        print(y)
    elif x == 3:
        print("statement3")
        y += input()
        print(y)
    elif x == 4:
        print("statement4")
        break

The if statements don't seem to be able to execute. After the value of x is printed it will loop back to statement0.

2
  • 3
    input() returns a str, you should convert x to an int before comparing it to other ints or compare it to strings Commented Jan 19, 2022 at 3:23
  • In other words, something like x == 1 can never be true in this code, at best it would be x == '1', but you could also just x = int(input()) (the difference is that in the latter case your script would end with an exception if the user enters something that's not a valid integer) Commented Jan 19, 2022 at 3:26

2 Answers 2

1

You have to first specify what will be the data type of the variable x.

  • Now as you are equating x to integers, you need to add int before the input() function.
  • Also, you have not initialized the variable y before, so python will throw an error. Define y beforehand with any integer value.
  • And try to keep the expressions inside parenthesis as it helps in easier understanding.

So, your final code becomes:

print("Test")

while True:
    print("statement0")

    x = int(input())
    print(x)

    y = 0  # initialize y with any value 

    if (x == 1):  # edit
        print("statement1")
        y += int(input())
        print(y)

    elif (x == 2):  # edit
        print("statement2")
        y += int(input())
        print(y)
    elif (x == 3):  # edit
        print("statement3")
        y += int(input())
        print(y)
    elif (x == 4):  # edit
        print("statement4")
        break
Sign up to request clarification or add additional context in comments.

2 Comments

I would upvote this answer, but one of the features of python I like best, is its conciseness. Putting () around those trivial expressions is overkill - the expressions are perfectly clear without that. I bet you learned a C-like language that requires parentheses, before learning Python! If so, that's the real reason it looks "better" to you with the parentheses - Familiarity. So did I, but I don't miss those parentheses when programming in python. :) I use parentheses in more complex expressions, to make the parts clearer. But in this case, its simply clutter, with no benefit.
@ToolmakerSteve Yeah you're right! They are much useful when there are complex expressions. I added those because they help make the code look cleaner and help visually organize the code in a logical way.
0

While taking the input, use x = int(input())

And, also y is not defined in this case, before doing something like y+=int(input()), you have to declare y above like y = 0

Thank you!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.