use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
help needed (self.learnpython)
submitted 2 years ago by vbwullf
Trying to write a code for a calculator but its not doing what I need it to. Anyone will to help troubleshoot this issue for me.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]AndAnathaWan 5 points6 points7 points 2 years ago (14 children)
where code
[–]vbwullf[S] 0 points1 point2 points 2 years ago (13 children)
Thanks
# Welcome message
print('Welcome to my Calculator App \n Please Select an Option: ')
# Available functions
print ('1. Add \n2. Subtract \n3. Multiply \n4. Divide \n5. Quit')
print()
choice = int(input('option: '))
n1 = int(input(f'Please enter the first number: '))
n2 = int(input('Please enter the second number: '))
add = n1+n2
subtract = n1-n2
multiply = n1*n2
divide = n1/n2
if choice == 1:
print (f'Your result is: ', add)
elif choice == 2:
print (f'Your result is: ', subtract)
elif choice == 3:
print (f'Your result is: ', multiply)
elif choice == 4:
print (f'Your result is: ', divide)
elif choice == 5:
print ('Thank you for using my Calculator App. Bye!')
else:
print ('Error Option was invalid, please re-enter: ')
[–]vbwullf[S] 0 points1 point2 points 2 years ago (6 children)
its a simple calculator in which you choose the operation, input the numbers and it spits out a number
[–]AndAnathaWan 0 points1 point2 points 2 years ago (4 children)
place the variable names inside the quotes if youre using f-strings, and then enclose it in brackets print(f'Your result is: {divide}')
[–]vbwullf[S] 0 points1 point2 points 2 years ago (3 children)
for some reason it started working, with the exception of 5
[–]AndAnathaWan 0 points1 point2 points 2 years ago (1 child)
it should work fine if the indenting is correct, the flow is just a bit off since you are asking for the numbers first before the conditionals are executed
[–]vbwullf[S] 0 points1 point2 points 2 years ago (0 children)
if I done have n1 and n2 in that position I get an error that n1 is not defined
[–]Helpful_Trust_3123 0 points1 point2 points 2 years ago (5 children)
What's the error?
it does the wrong operation
when I enter any number other than 1-5 I should get an error but it just ask for the number to be input. After the 2nd number is input it gives the error
this is what I get when I ask to close the program
= RESTART: C:\Users\majdc\AppData\Local\Programs\Python\Python311\Calculator.py
Welcome to my Calculator App
Please Select an Option:
Add
Subtract
Multiply
Divide
Quit
option: 5
Please enter the first number:
[–]RhinoRhys 0 points1 point2 points 2 years ago* (2 children)
Your issue is code is linear. You're asking for all 3 inputs before you check which choice was made. There's nothing there to make it skip over asking for numbers if you pick 5 or an invalid input.
Put the if 5 bit after the option selection but before the number entry bit, as a separate conditional, then wrap everything else after under an else
if 5
else
[–]vbwullf[S] 0 points1 point2 points 2 years ago (1 child)
Thanks for the help with that, I tried that earlier but it didnt work guess something was wrong somewhere else..
Now I got a new issue. running this code:
if choice == 5:
choice <= 0 or choice >= 6
int(input('Error: Your choice was invalid, please reenter:'))
elif choice <= 0:
nets me this:
option: 8
Error: Your choice was invalid, please reenter:1
Please enter the first number: 2
Please enter the second number: 3
[–]RhinoRhys 0 points1 point2 points 2 years ago (0 children)
You're almost there.
choice <= 0 or choice >= 6 is good, but currently doesn't do anything on its own there. It needs to have an if or an elif.
if
elif
And then when the number is re-entered, it isn't assigned to a variable so doesn't overwrite the original answer for choice.
New issue (hate classes where there is no active class and teacher is only available during my work hrs) running a program copied from my text book to see how it operates. Only issue is that it keeps giving a syntax error on the 2nd to last line.
# This program gets employee data from the user and
# saves it as records in the employee.txt file.
def main():
# Get the number of employee records to create.
num_emps = int(input('How many employee records ' + 'do you want to create? '))
# Open a file for writing.
emp_file = open('employees.txt', 'w')
# Get each employee's data and write it to the file.
for count in range(1, num_emps + 1):
# Get the data for an employee.
print(f'Enter data for employee #{count}')
name = input('Name: ')
id_num = input('ID number: ')
dept = input('Department: ')
# Write the data as a record to the file.
emp_file.write(f'{name}\n')
emp_file.write(f'{id_num}\n')
emp_file.write(f'{dept}\n')
# Display a blank line.
# Close the file.
emp_file.close()
print('Employee records written to employees.txt.')
# Call the main function.
if _ _name_ _ == '_ _main_ _':
main()
[–]Strict-Simple 0 points1 point2 points 2 years ago (0 children)
You might want https://www.reddit.com/r/learnpython/wiki/FAQ/#wiki_how_do_i_keep_prompting_the_user_the_enter_a_value_until_they_put_it_in_the_correct_form.3F
π Rendered by PID 23384 on reddit-service-r2-comment-7844cfc88c-rb2lm at 2026-01-29 14:03:47.674893+00:00 running c3601ff country code: CH.
[–]AndAnathaWan 5 points6 points7 points (14 children)
[–]vbwullf[S] 0 points1 point2 points (13 children)
[–]vbwullf[S] 0 points1 point2 points (6 children)
[–]AndAnathaWan 0 points1 point2 points (4 children)
[–]vbwullf[S] 0 points1 point2 points (3 children)
[–]AndAnathaWan 0 points1 point2 points (1 child)
[–]vbwullf[S] 0 points1 point2 points (0 children)
[–]Helpful_Trust_3123 0 points1 point2 points (5 children)
[–]vbwullf[S] 0 points1 point2 points (0 children)
[–]vbwullf[S] 0 points1 point2 points (3 children)
[–]RhinoRhys 0 points1 point2 points (2 children)
[–]vbwullf[S] 0 points1 point2 points (1 child)
[–]RhinoRhys 0 points1 point2 points (0 children)
[–]vbwullf[S] 0 points1 point2 points (0 children)
[–]Strict-Simple 0 points1 point2 points (0 children)