Этот скрипт всегда говорит, что я сделал неправильный выбор. Если заявление может быть проигнорировано - PullRequest
0 голосов
/ 25 февраля 2020

Я пытался заставить этот калькулятор работать, но он всегда говорит, что мой выбор неверен. Я попробовал действительные числа, и по некоторым причинам это не работает. Любая помощь будет оценена. Я все еще новичок в python и в программировании в целом. Спасибо.

#Calculator
print("1.Addition")
print("2.Substraction")
print("3.Multiplication")
print("4.Divison")
#Choose the calculation
Choice=int(input("Enter your choice (1/2/3/4): "))
#Inserting the numbers
Num1=float(input("Insert your first number: "))
Num2=float(input("Insert your second number: "))

if Choice == '1':
    ans= Num1 + Num2
    print("Your answer is: ",ans)

elif Choice == '2':
    ans= Num1 - Num2
    print("Your answer is: ",ans)

elif Choice == '3':
    ans= Num1*Num2
    print("Your answer is: ",ans)

elif Choice == '4':
    ans= Num1/Num2
    print("Your answer is: ",ans)

else:
    print("Invalid choice. ")

Ответы [ 3 ]

2 голосов
/ 25 февраля 2020

Измените условия if, заменив строки самими числами:

if Choice == 1:
    ans= Num1 + Num2
    print("Your answer is: ",ans)

elif Choice == 2:
    ans= Num1 - Num2
    print("Your answer is: ",ans)

elif Choice == 3:
    ans= Num1*Num2
    print("Your answer is: ",ans)

elif Choice == 4:
    ans= Num1/Num2
    print("Your answer is: ",ans)

else:
    print(f"Invalid choice.")
1 голос
/ 25 февраля 2020

проблема в том, что вы проверяете, эквивалентен ли ответ STRING, а не целое число!

#Calculator
print("1.Addition")
print("2.Substraction")
print("3.Multiplication")
print("4.Divison")
#Choose the calculation
choice=int(input("Enter your choice (1/2/3/4): "))
#Inserting the numbers1
Num1=int(input("Insert your first number: "))
Num2=int(input("Insert your second number: "))

if choice == 1:
    ans= Num1 + Num2
    print("Your answer is: ",ans)
elif choice == 2:
    ans= Num1 - Num2
    print("Your answer is: ",ans)
elif choice == 3:
    ans= Num1*Num2
    print("Your answer is: ",ans)
elif choice == 4:
    ans= Num1/Num2
    print("Your answer is: ",ans)
else:
    print("Invalid choice. ")
0 голосов
/ 25 февраля 2020
Choice=int(input("Enter your choice (1/2/3/4): "))

Choice является int.

if Choice == '1':

Вы сравниваете его со строкой ('1'). Так что ваши операторы if не будут работать.

Попробуйте:

if Choice == 1:
...