Прервите завершение программы вместо повторного запуска функции whilePython

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Прервите завершение программы вместо повторного запуска функции while

Сообщение Anonymous »


I was creating a basic calculator. In this calculator you have to enter two numbers and operator(to divide multiply add or subtract). I wanted to make it so that if you entered a non-integer in the number selection then it would've told you that your input is invalid and break(I assumed this would repeat the loop but it ends the program if you enter a non-integer).
Here is the code:

Код: Выделить всё

from math import *

calculatorCheck = 0
operator = 0

while calculatorCheck == 0:
try:
firstNumber = int(input("Enter first number: "))
except ValueError:
print("Invalid input. Please try again.")
break
try:
operator = input("Enter operator: ")
except operator != "*"  or operator != "/" or operator != "+" or operator != "-":
print("Invalid operator. Please try again")
break
try:
secondNumber = int(input("Enter second number: "))
except ValueError:
print("Invalid input. Please try again.")
break
calculatorCheck == 1
break

if operator == "*":
print(firstNumber*secondNumber)
elif operator == "/":
print(firstNumber/secondNumber)
elif operator == "+":
print(firstNumber+secondNumber)
elif operator == "-":
print(firstNumber-secondNumber)
I searched the interned for "Python break ending the program" but no results that had to do with my problem popped up.


Источник: https://stackoverflow.com/questions/781 ... tion-again
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»