sidick angel
Badges: 7
Rep:
?
#1
Report Thread starter 9 months ago
#1
month= int(input("month"))
When I type that as my code and as input I enter 08 it gives out an error.
Does anyone know who I can fix that please.
0
reply
winterscoming
Badges: 19
Rep:
?
#2
Report 9 months ago
#2
(Original post by sidick angel)
month= int(input("month"))
When I type that as my code and as input I enter 08 it gives out an error.
Does anyone know who I can fix that please.
Are you using Python 2? if so, you need to use raw_input instead.
Code:
month = int(raw_input("month"))

The problem is that the leading zero means Python 2 thinks you're writing an octal number, so 08 isn't valid. Octal digits are only valid from 0 upto 7. But furthermore, the decimal number 8 would be octal 010, then decimal 9 would be 011, and decimal 10 would be 012, etc.

(If you've got the option to use Python 3 instead of Python 2, then it would work just with plain 'input')
Last edited by winterscoming; 9 months ago
0
reply
sidick angel
Badges: 7
Rep:
?
#3
Report Thread starter 9 months ago
#3
year= int(input("enter year"))
check_year= year[2:4]

I did that first but it gave an error "TypeError: 'int' object is not subscriptable" .
I then tried it as check_year= str(year[2:4]) but it still doesn't work.
How can I solve that?
Last edited by sidick angel; 9 months ago
0
reply
winterscoming
Badges: 19
Rep:
?
#4
Report 9 months ago
#4
(Original post by sidick angel)
year= int(input("enter year"))
check_year= year[2:4]

I did that first but it gave an error "TypeError: 'int' object is not subscriptable" .
I then tried it as check_year= str(year[2:4]) but it still doesn't work.
How can I solve that?
What is the user actually typing when they're asked to enter the year? And what is check_year supposed to do? are you just trying to get the last 2 digits of the year?

If it's just a number such as 2019 then all you need is year = int(raw_input("enter year"))

Given that 2019 is just a number, if you want to get the last 2 digits of the year, then you'll just need to do an arithmetic calculation such as mod 100 to get the remainder of 2019/100 (which would be 19)


You can't use the square brackets with a number - the square brackets are used when you've got text strings instead. One of the 'gotcha's with using "input" in Python2 is that it tries to automatically decide whether the thing you've got is a number, so raw_input is easier because you can guarantee it'll always give you a text string, and then you can decide whether you want to convert it to an int or not.

So you have several options such as this: https://repl.it/repls/StrictSilentCurrencies
Code:
year_string = raw_input("enter year")
year = int(year_string)
check_year = year % 100
print(check_year)
Or this: https://repl.it/repls/PalegreenPeskyQbasic
Code:
year_string = raw_input("enter year")
check_year = year_string[2:4]
print(check_year)
Remember that raw_input always gives you a str, so it's easier to work that way.

You will see lots of examples online for Python 3 that use input() instead -- but that's because the Python3 input works in the same way as Python 2's raw_input.
Last edited by winterscoming; 9 months ago
0
reply
sidick angel
Badges: 7
Rep:
?
#5
Report Thread starter 9 months ago
#5
thanks.
0
reply
sidick angel
Badges: 7
Rep:
?
#6
Report Thread starter 8 months ago
#6
Hello can someone help me out again?
0
reply
sidick angel
Badges: 7
Rep:
?
#7
Report Thread starter 8 months ago
#7
num1= int(input("enter first num:"))
num2=int(input("enter second num:"))
symbol= int(input("enter symbol"))
ans= num1+ symbol+ num2
print(num1+ symbol+num2 + '='+ ans)
0
reply
sidick angel
Badges: 7
Rep:
?
#8
Report Thread starter 8 months ago
#8
This was my code but I do not want to set the operations using selection statements since earlier on.
I want to do it according to what the user enters. For example:
If the user enters 5 and 4 together with a +,
the program automatically does the calculation and gives out an answer of 9.
I tried that but it does not work with that data type in python.
how can i do it?
0
reply
winterscoming
Badges: 19
Rep:
?
#9
Report 8 months ago
#9
(Original post by sidick angel)
num1= int(input("enter first num:"))
num2=int(input("enter second num:"))
symbol= int(input("enter symbol"))
ans= num1+ symbol+ num2
print(num1+ symbol+num2 + '='+ ans)
(Original post by sidick angel)
This was my code but I do not want to set the operations using selection statements since earlier on.
I want to do it according to what the user enters. For example:
If the user enters 5 and 4 together with a +,
the program automatically does the calculation and gives out an answer of 9.
I tried that but it does not work with that data type in python.
how can i do it?
Firstly, the + symbol isn't an int it's just a plain character, so I would expect your program will error on this line when you type a non-numeric character:
Code:
symbol= int(input("enter symbol"))
If you get rid of the int() part of that line it ought to be fine.
Code:
symbol = input("enter symbol")


Next, to join those variables together, I'd recommend using string format() so that you don't need to worry about type casting and can build up the mathematical expression more easily - e.g. https://stackoverflow.com/questions/...ting-in-python
Code:
num1 = int(input("enter first num:"))
num2 = int(input("enter second num:"))
symbol = input("enter symbol")
expression = '{0} {1} {2}'.format(num1, symbol, num2)
Lastly, if you want to get the result of a dynamic mathematical like that, then you need to use python's eval() function
https://docs.python.org/3/library/functions.html#eval
As long as the expression is a valid Python expression, then using 'eval' will execute it and yield the result:
Code:
ans = eval(expression)
print(expression,'=', ans)
Here's an example with those bits working together: https://repl.it/repls/CautiousPureUsernames
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Are you worried that a cap in student numbers would affect your place at uni?

Yes (108)
59.67%
No (38)
20.99%
Not sure (35)
19.34%

Watched Threads

View All