Jak začít novou operaci v Pythonu

0

Otázka

Takže mám jednoduchý kalkulačka v Pythonu. Takže to, co to dělá, je, žádá o operace (jako sčítání) a se zeptá na první číslo a druhé číslo.

Řekněme, že si vyberu toho,

první číslo: 1

druhé číslo: 1

výsledek: 2

A poté, co že to chci se zeptat: typ {x} začít nový výpočet

Když jste typ x je to v podstatě restartuje všechno, takže si můžete dělat různé výpočty. ( {x} může to být cokoliv, mě to nevadí)

Jak to mám udělat, že?

současný kód:

print("Which operation do you want to do?")
print("Type + for addition")
print("Type - for subtraction")
print("Type * for multiplication")
print("Type / for division")

op = input('Enter your choice here = ')


if op == '+' :
  num1 = float(input("Enter the first number: "))
  num2 = float(input("Enter the second number here: "))
  add = num1 + num2

  print("{0} + {1} is {2}".format(num1, num2, add))


elif op == '-' :
  num1 = float(input("Enter the first number here: "))
  num2 = float(input("Enter the second number here: "))
  sub = num1 - num2

  print("{0} - {1} is {2}".format(num1, num2, sub))


elif op == '*' :
  num1 = float(input("Enter the first number here: "))
  num2 = float(input("Enter the second number here: "))
  multi = num1 * num2

  print("{0} * {1} is {2}".format(num1, num2, multi))


elif op == '/' :
  num1 = float(input("Enter the first number here: "))
  num2 = float(input("Enter the second number here: "))
  division = num1 / num2

  print("{0} / {1} is {2}".format(num1, num2, division))


else :
  print("something went wrong!")
3
0

můžete umístit kód uvnitř funkce a pak volání funkce uvnitř while smyčky, druhý vstup again slouží k přerušení smyčky, jakmile uživatel dokončení

print("Which operation do you want to do?")
print("Type + for addition")
print("Type - for subtraction")
print("Type * for multiplication")
print("Type / for division")


def function(op):
    if op == '+' :
        num1 = float(input("Enter the first number: "))
        num2 = float(input("Enter the second number here: "))
        add = num1 + num2
    
        print("{0} + {1} is {2}".format(num1, num2, add))
    
    
    elif op == '-' :
        num1 = float(input("Enter the first number here: "))
        num2 = float(input("Enter the second number here: "))
        sub = num1 - num2
    
        print("{0} - {1} is {2}".format(num1, num2, sub))
    
    
    elif op == '*' :
        num1 = float(input("Enter the first number here: "))
        num2 = float(input("Enter the second number here: "))
        multi = num1 * num2
    
        print("{0} * {1} is {2}".format(num1, num2, multi))
    
    
    elif op == '/' :
        num1 = float(input("Enter the first number here: "))
        num2 = float(input("Enter the second number here: "))
        division = num1 / num2
    
        print("{0} / {1} is {2}".format(num1, num2, division))
    
    
    else:
        print("something went wrong!")

while True:
    op = input('Enter your choice here = ')
    function(op)
    again = input('Enter X to start new calculation: ')
    if again.lower() != 'x':
        break
2021-11-23 11:43:26
0
while True:
    print("Which operation do you want to do?")
    print("Type + for addition")
    print("Type - for subtraction")
    print("Type * for multiplication")
    print("Type / for division")
    print("Type 0 to exit")

    op = input('Enter your choice here = ')

    if op == '+' :
        while True:
            num1 = float(input("Enter the first number: "))
            num2 = float(input("Enter the second number here: "))
            add = num1 + num2
            print("{0} + {1} is {2}".format(num1, num2, add))

            again = input("Would you like to add two other numbers? Y/N: ").capitalize().strip()
            if again == 'Y':
                continue
            else:
                break


    elif op == '-' :
        while True:
            num1 = float(input("Enter the first number here: "))
            num2 = float(input("Enter the second number here: "))
            sub = num1 - num2
            print("{0} - {1} is {2}".format(num1, num2, sub))
            again = input("Would you like to subtract two other numbers? Y/N: ").capitalize().strip()
            if again == 'Y':
                continue
            else:
                break


    elif op == '*' :
        while True:
            num1 = float(input("Enter the first number here: "))
            num2 = float(input("Enter the second number here: "))
            multi = num1 * num2
            print("{0} * {1} is {2}".format(num1, num2, multi))
            again = input("Would you like to multiply two other numbers? Y/N: ").capitalize().strip()
            if again == 'Y':
                continue
            else:
                break


    elif op == '/' :
        while True:
            num1 = float(input("Enter the first number here: "))
            num2 = float(input("Enter the second number here: "))
            division = num1 / num2
            print("{0} / {1} is {2}".format(num1, num2, division))
            again = input("Would you like to divide two other numbers? Y/N: ").capitalize().strip()
            if again == 'Y':
                continue
            else:
                break

    elif op == '0':
        print("Good bye!")
        break
    else :
        print("something went wrong!")
2021-11-23 11:47:29
0

Můžete použít cyklus while k tomu. Použití booleovské proměnné opakovat rozhodnout, zda chce uživatel provést další výpočet. Pokud vstup uživatele je " x " opakování zůstává pravda a kalkulačka je restartován, ale pokud uživatel zadá něco jiného, opakování je False a smyčka ukončí:

repeat = True
while repeat:
    #Your code here
    again = input('type x to start new calculation')
    repeat = again in ['x', 'X']
2021-11-23 11:42:16

V jiných jazycích

Tato stránka je v jiných jazycích

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................