Code error from Fundamentals of Python Programming van Richard L. Halterman - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Code error from Fundamentals of Python Programming van Richard L. Halterman (/thread-40329.html) Pages:
1
2
|
Code error from Fundamentals of Python Programming van Richard L. Halterman - Heidi - Jul-12-2023 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] Can someone tell me what i'am doing wrong?
RE: Code error from Fundamentals of Python Programming van Richard L. Halterman - DPaul - Jul-12-2023 Hi, It's simple: 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 RE: Code error from Fundamentals of Python Programming van Richard L. Halterman - Heidi - Jul-12-2023 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) ERROR!
RE: Code error from Fundamentals of Python Programming van Richard L. Halterman - menator01 - Jul-12-2023 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. RE: Code error from Fundamentals of Python Programming van Richard L. Halterman - DPaul - Jul-12-2023 (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 RE: Code error from Fundamentals of Python Programming van Richard L. Halterman - Heidi - Jul-12-2023 (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') 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 RE: Code error from Fundamentals of Python Programming van Richard L. Halterman - DPaul - Jul-12-2023 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 RE: Code error from Fundamentals of Python Programming van Richard L. Halterman - Heidi - Jul-12-2023 (Jul-12-2023, 05:25 AM)DPaul Wrote: Hi, 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 RE: Code error from Fundamentals of Python Programming van Richard L. Halterman - Heidi - Jul-12-2023 (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. 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. RE: Code error from Fundamentals of Python Programming van Richard L. Halterman - deanhystad - Jul-12-2023 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. 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. |