What’s wrong with this Python code?

Asked

Viewed 92 times

-1

So I’m learning Python from the video course and I’m in class 9, Ex23, and I’m trying (in a mathematical way) to show the numbers divided to the person, only he’s wrong, and I have no idea what to do here, and even with Pycharm telling me that something’s wrong, I don’t understand what’s wrong:

Nam = input(int('Qual o numero (de 1 ate 9999)?'))
M = (Nam//1000)
C = Nam-(M*1000)
C = C//100
D = Nam-((M*1000)+(C*100))
D = D//10
lol = Nam-((M*1000)+(C*100)+(D*10))
print(M)
print(C)
print(D)
print(lol)

Pycharm keeps telling me there’s something wrong with "Nam," but I don’t know what it is

  • 5

    Would not be: Nam = int(input('Qual o numero (de 1 ate 9999)?')), reads a string and converts what was read to int?

  • int('texto') tries to convert the text to number, and since the text "Which is the number etc" is not a number, gives error. It is right to first get the text from the input and then try to convert to number (ie, int(input(texto)), as stated above)

1 answer

2

Just swap the int with the place input. Your code at the moment is like this:

Nam = input(int('Qual o numero (de 1 ate 9999)?'))

The correct one would be to define the type of variable (str, Flot, int, bool) before the input:

Nam = int(input('Qual o numero (de 1 ate 9999)?'))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.