-5
print('Select operation')
print('Choose from:')
print('+')
print('-')
print('*')
print('/')

choice=input('Enter choice (+,-,*,/):')

num1=int(input('Enter first number:'))
num2=int(input('Enter second number:'))

if choice== '+':
print(num1,'+',num1,'=', (num1+num2))
while restart **=** input('Do you want to restart the calculator y/n'):
    if restart == 'y':t
        print('restart')
        else restart == 'n':
            print('Thanks for using my program')
            break

 elif choice== '-':
print(num1,'-',num2,'=', (num1-num2))

elif choice== '*':
print(num1,'*',num2,'=', (num1*num2))

elif choice== '/':
print(num1,'/',num2,'=',(num1/num2))

else:
print('Invalid input')

What is wrong with the = in bold? I don't understand what is wrong with it? Someone please answer my question.

Thank you, Charlotte

Charlotte
  • 1
  • 4

2 Answers2

0

You've tried to use an assignment statement as a Boolean value; this fails on a couple of counts. Most of all, you're spreading your restart logic across several lines of code, and confusing the parser.

You likely want something like this:

restart = input('Do you want to restart the calculator y/n')
while restart.lower() == 'y':
    ...
    restart = input('Do you want to restart the calculator y/n')
Prune
  • 76,765
  • 14
  • 60
  • 81
0

Multiple issues here:

  1. assignment from input into a variable cannot be checked in while-loop, you should split it into assignment and check.

  2. else cannot contain a condition

  3. you also had a bug in printing the results - you printed num1 twice

  4. The indentations are meaningful in Python - please make sure to post it properly indented next time

A fix for the issues above:

def calc():
    print('Select operation')
    print('Choose from:')
    print('+')
    print('-')
    print('*')
    print('/')

    choice=input('Enter choice (+,-,*,/):')

    num1=int(input('Enter first number:'))
    num2=int(input('Enter second number:'))
    if choice == '+':
        print("{}+{}={}".format(num1, num2, num1+num2))

    elif choice == '-':
        print("{}-{}={}".format(num1, num2, num1-num2))

    elif choice == '*':
        print("{}*{}={}".format(num1, num2, num1*num2))

    elif choice == '/':
        print("{}/{}={}".format(num1, num2, num1/num2))
    else:
        print('Invalid input')


if __name__ == '__main__':
    restart = 'y'
    while restart:
        if restart == 'y':
            print('restart')
            calc()
            restart = input('Do you want to restart the calculator y/n')    
        elif restart == 'n':
            print('Thanks for using my program')
            break
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • the "restart the calculator" loop is infinite. – Jean-François Fabre Jun 20 '17 at 20:19
  • @Jean-FrançoisFabre - fixed – Nir Alfasi Jun 20 '17 at 20:24
  • @Charlotte pay attention - I refactored the code a bit by extracting the business logic of getting the input and doing the calculation into a function and calling that function from main. You can (and should) keep refactoring it and read (and validate) the input in a dedicated function - and do the calculation in a different one. – Nir Alfasi Jun 20 '17 at 20:26
  • this kind of construct calls for a `while True` to avoid to force `restart` to `y` at startup. `while restart` doesn't make sense unless you want the program to quit silently when the user enters an empty string. – Jean-François Fabre Jun 20 '17 at 20:27
  • @Jean-FrançoisFabre I believe it's a personal preference, I don't have a strong opinion either way. – Nir Alfasi Jun 20 '17 at 20:29