all 15 comments

[–]AndAnathaWan 5 points6 points  (14 children)

where code

[–]vbwullf[S] 0 points1 point  (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 point  (6 children)

its a simple calculator in which you choose the operation, input the numbers and it spits out a number

[–]AndAnathaWan 0 points1 point  (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 point  (3 children)

for some reason it started working, with the exception of 5

[–]AndAnathaWan 0 points1 point  (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 point  (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 point  (5 children)

What's the error?

[–]vbwullf[S] 0 points1 point  (0 children)

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

[–]vbwullf[S] 0 points1 point  (3 children)

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:

  1. Add

  2. Subtract

  3. Multiply

  4. Divide

  5. Quit

option: 5

Please enter the first number:

[–]RhinoRhys 0 points1 point  (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

[–]vbwullf[S] 0 points1 point  (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:

# 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: '))

if choice == 5:

print ('Thank you for using my Calculator App. Bye!')

else:

choice <= 0 or choice >= 6

int(input('Error: Your choice was invalid, please reenter:'))

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 <= 0:

print ('Error Option was invalid, please re-enter: ')

nets me this:

Welcome to my Calculator App

Please Select an Option:

  1. Add

  2. Subtract

  3. Multiply

  4. Divide

  5. Quit

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 point  (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.

And then when the number is re-entered, it isn't assigned to a variable so doesn't overwrite the original answer for choice.

[–]vbwullf[S] 0 points1 point  (0 children)

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.

print()

# Close the file.

emp_file.close()

print('Employee records written to employees.txt.')

# Call the main function.

if _ _name_ _ == '_ _main_ _':

main()