Python Forum
Code error from Fundamentals of Python Programming van Richard L. Halterman
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code error from Fundamentals of Python Programming van Richard L. Halterman
#1
Hi I'm a novice and then maybe asking a stupid question for most of you. I am doing a study using the book Fundamentals of Python Programming by Richard L. Halterman. When I literally copy a code from the book and put it in wing 101, I always get an error.

This is the simple code, I only put the value 2 and 17

x = input('Please enter an integer value: 2')
y = input('Please enter another integer value: 17')
num1 = int(x)
num2 = int(y)
print(num1, '+', num2, '=', num1 + num2)
This is what happen when i put on run

Python 3.11.4 (tags/v3.11.4:d2340ef, Jun  7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)]
Type "help", "copyright", "credits" or "license" for more information.
[evaluate untitled-2.py]
Please enter an integer value: 2
Please enter another integer value: 17
Traceback (most recent call last):
  File "C:\Users\hioos\untitled-2.py", line 3, in <module>
    num1 = int(x)
builtins.ValueError: invalid literal for int() with base 10: ''
Next:

num1 = int(input('Please enter an integer value: 2'))
num2 = int(input('Please enter another integer value: 17'))
print(num1, '+', num2, '=', num1 + num2)
My output:

[evaluate untitled-2.py]
Error:
Please enter an integer value: 2 Traceback (most recent call last): File "C:\Users\hioos\untitled-2.py", line 1, in <module> num1 = int(input('Please enter an integer value: 2')) builtins.ValueError: invalid literal for int() with base 10: ''
Can someone tell me what i'am doing wrong?
Gribouillis write Jul-12-2023, 06:31 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Hi,
It's simple:
Output:
x = input('Please enter an integer value: 2')
You have inserted the answer "2", inside the input question string.
x = input('Please enter an integer value: ')
If you execute the program and enter 2 and 17 after the respective questions, it will work.
Remove the 2 and 17 from the question, and leave a space instead.
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
Can you please show me that in a complete code? I really do not understand what i'am doing wrong. You wrote i have to put the answer in the string but i thought that i do that. The first 2 line work but after that becomes the error. I really don't see it. I'l been trying it for days but i can't get further with my study before i understand this. Can you please show me it into a Code? And what i have to do step by step?

x = input('Please enter an integer value: 2 ')
y = input('Please enter an integer value: 7 ')
num1 = int(x)
num2 = int(y)
print(num1, '+', num2, '=', num1 + num2)
Output:
Please enter an integer value: 2 Please enter an integer value: 7

ERROR!
Error:
Traceback (most recent call last): File "<string>", line 5, in <module> ValueError: invalid literal for int() with base 10: ''
Larz60+ write Jul-12-2023, 05:07 PM:
2nd request:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Again fixed. Please use bbcode tags on future posts, it's forum rules.
Reply
#4
Not sure exactly what you're trying to do. input takes a users input and store it in variable x. Y does the same thing. If you are not entering a number value for x and y it will error.

If want a default value you could do something like this

x = input('Please enter an integer value: 2') or 2
y = input('Please enter another integer value: 17') or 17
num1 = int(x)
num2 = int(y)
print(num1, '+', num2, '=', num1 + num2)
If you're not wanting user input just store it in a variable.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#5
(Jul-12-2023, 07:30 AM)Heidi Wrote: I really do not understand what i'am doing wrong.
You ask for a user input: x = input('Please enter an integer value: 2')
but a the same time you provide a suggested answer: "2".
You imagine that the system will accept 2, but it is part of the question string.
You need to do as menator01 suggests, or the simple way:
x = input('Please enter an integer value: ')
and type the answer yourself.
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#6
(Jul-12-2023, 08:35 AM)DPaul Wrote:
(Jul-12-2023, 07:30 AM)Heidi Wrote: I really do not understand what i'am doing wrong.
You ask for a user input: x = input('Please enter an integer value: 2')
but a the same time you provide a suggested answer: "2".
You imagine that the system will accept 2, but it is part of the question string.
You need to do as menator01 suggests, or the simple way:
x = input('Please enter an integer value: ')
and type the answer yourself.
Paul

Thank you for the Answer, I think i have to quit the study. I don't even understand your answer. I need to see it maybe step by step. I realy don't uderstand it wat i have to do. But thank you
Reply
#7
OK,
Your original code will be perfect, when you delete "2" and "17".
And run again.
It will work.
x = input('Please enter an integer value: ')
y = input('Please enter another integer value: ')
num1 = int(x)
num2 = int(y)
print(num1, '+', num2, '=', num1 + num2)
Paul
Heidi likes this post
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#8
(Jul-12-2023, 05:25 AM)DPaul Wrote: Hi,
It's simple:
Output:
x = input('Please enter an integer value: 2')
You have inserted the answer "2", inside the input question string.
x = input('Please enter an integer value: ')
If you execute the program and enter 2 and 17 after the respective questions, it will work.
Remove the 2 and 17 from the question, and leave a space instead.
Paul

I Think now i did it right. I have to give they answer in the shell Part? I did that and than i don't became a error
Reply
#9
(Jul-12-2023, 07:50 AM)menator01 Wrote: Not sure exactly what you're trying to do. input takes a users input and store it in variable x. Y does the same thing. If you are not entering a number value for x and y it will error.

If want a default value you could do something like this

x = input('Please enter an integer value: 2') or 2
y = input('Please enter another integer value: 17') or 17
num1 = int(x)
num2 = int(y)
print(num1, '+', num2, '=', num1 + num2)
If you're not wanting user input just store it in a variable.

Thank you,
When i put or 2 and or 17 behind the string than it workd. It works either when i execute the first and put the answer in the shelll and than the second.
Reply
#10
There is nothing wrong with the first program. It would work fine if you entered values when the program is run. This is me running your program.
Output:
(env)> python test.py Please enter an integer value: 2 42 Please enter another integer value: 17 24 42 + 24 = 66
I entered 42 and 24, and the program printed the correct sum.

The question is, why do you have "2" and "17" in you input prompts?

The input(prompt) function takes keyboard input from the user. It prints the prompt and waits for the user to press the enter key. Any keys typed before the enter key are accepted as input and returned by the function. If you don't type anything before pressing enter, the function returns an empty string (''). Your prompt makes it look like input was typed 2 or 17, but they do not provide any input. You have to type in the values when the program is running.
Skaperen likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax error while executing the Python code in Linux DivAsh 8 1,624 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,583 Mar-27-2023, 07:38 AM
Last Post: buran
  Error in if-then-else python code Led_Zeppelin 6 2,397 Jul-27-2022, 07:53 PM
Last Post: deanhystad
  Programming robots using Python OscarBoots 5 3,465 Oct-31-2021, 09:38 AM
Last Post: Larz60+
  Facing error while executing below Python code ramu4651 1 5,741 Jan-26-2021, 06:40 PM
Last Post: ibreeden
  Skeleton file export error Python Code pepapoha 4 3,514 Nov-17-2020, 02:06 AM
Last Post: pepapoha
  Compiling Python 3.8.5 source code results in build error Deepan 0 2,194 Sep-14-2020, 04:11 AM
Last Post: Deepan
  Syntax error in python code sample ErnestTBass 5 3,133 Aug-14-2020, 07:14 PM
Last Post: deanhystad
  Error in Python Code ErnestTBass 4 2,748 Jun-04-2020, 05:28 PM
Last Post: buran
  Python Code error Riteshfrancis 2 2,442 May-21-2020, 06:37 AM
Last Post: Riteshfrancis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020