Converting strings into integer numbers

Hi, I am new to the python course. I downloaded the latest version of python and have it installed. Now I tried to work on the excercise 2 from the intermediate expression course. I don´t know but somehow the way Dr. Severance showed how to convert strings into integer doesn´t work when I tried it myself. Does anyone has an idea?

Here´s how I write the sequence:

x=input('Enter Hours: ')
h=int(x)
y=input('Enter Rates: ')
r=int(y)
pay=h*r
print(pay)

Ok, thx everybody for help! Best regards!

What makes you think this doesn’t work?

Hi Sanity,

I know it doesn´t work because whenever I type this into py there is an error message!

Traceback (most recent call last):
File “”, line 1, in
ValueError: invalid literal for int() with base 10: ‘’

That depends what’s written as input when asked. int won’t be able to convert string to integer if string will contain anything else than numbers.

No, even when I type in a number py doesn´t accept it as an integer

How are you trying to run it? Is this code saved in file, or are you writing it into python interpreter?

I just wrote it into py interpreter. Should I save it in file first? Because I´m not that far in the py course where they talk about saving anything in files

Either is fine, just different actions might be needed to figure out what’s happening.

Can you check what’s the x variable after it’s entered with the line below? If this is interpreter then either just using variable name, or printing it out, should show the value.

x = input('Enter Hours: ')

Hi sanity, I solved the problem

the thing is that I have to put the function int in the same line as the input line

like this

x=int(input('Enter Hours: '))

Technically you don’t. I can run your original code without problem.
Whatever the problem, it’s not in the code.

1 Like

Hm, maybe it is because I type it directly in the python interpreter instead of using a file?!

You cannot write in the interpreter - the interpreter is a program that is interpreting text as Python code. You can only use different environments to write that text. Like the Python-IDLE shell is propably the most basic thing. But even there I can run your code line by line.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.