The Student Room Group

python

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.
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.
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')
(edited 4 years ago)
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?
(edited 4 years ago)
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

year_string = raw_input("enter year")
year = int(year_string)
check_year = year % 100
print(check_year)


Or this: https://repl.it/repls/PalegreenPeskyQbasic

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.
(edited 4 years ago)
thanks.
Hello can someone help me out again?
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)
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?
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:
symbol= int(input("enter symbol"))
If you get rid of the int() part of that line it ought to be fine.
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/517355/string-formatting-in-python
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:
ans = eval(expression)
print(expression,'=', ans)


Here's an example with those bits working together: https://repl.it/repls/CautiousPureUsernames

Quick Reply

Latest