我有一个事实,当我在开始#Restart时写“ n”时,理想情况下应该写“ Goodbye”并在10秒后关闭,但是它会启动else过程:最后,然后重新启动一切。但我输入的所有内容正确无误,我称之为“输入错误!”的错误不应该,只是可以帮助谁,我不知道为什么会这样.-。
------------------------------------------------ - - - - - - - - - 开始 - - - - - - - - - - - - - - - - -----------------------------
1-俄语,2-英语,3-UA? (无空格):2
判别式源自根?(y / n):y
输入数字a:1
输入数字b:2
输入数字c:3
判别式= -8.0
无根!
如果在该点之后有许多数字,那么答案要么是分数,要么答案不正确。
您要继续吗?(y / n):n
再见!
输入错误!
正在重新启动...
1-俄语,2-英语,3-UA? (无空格):
------------------------------------------------ - - - - - - - - 结束 - - - - - - - - - - - - - - - - - ---------------------------
代码开头:
While True:
#Language
language = input("1-Russian, 2-English, 3-UA? (Without spaces):")
#English
if language == "2":
while True:
#Output from the root
kor = input( "The discriminant is derived from the root?(y/n):")
#a,b,c
try:
a = float( input( "Enter a number a:"))
b = float( input("Enter a number b:"))
c = float( input( "Enter a number c:"))
except (ValueError):
import time
print ("Input error! This is not a number")
print ("Restarting...")
time.sleep(1)
continue
#Finding D
D = (b * b) - 4 * a * c
#Finding x1, x2 and print this
if kor == "y":
if D > 0:
import math
xone = (-b + (math.sqrt(D))) / (2 * a)
xtwo = (-b - (math.sqrt(D))) / (2 * a)
print ( "Discriminant = " + str(D))
print ( "x1 = " + str(xone))
print ( "x2 = " + str(xtwo))
print ( "If there are many numbers after the point then the answer is either a fraction or the answer is not correct.")
if D == 0:
import math
x = -b / (2 * a)
print ( "Disctiminant = " + str(D))
print ( "x = " + str(x))
print ( "If there are many numbers after the point then the answer is either a fraction or the answer is not correct.")
if D < 0:
import math
print ( "Discriminant = " + str(D))
print ( "No roots! " )
print ( "If there are many numbers after the point then the answer is either a fraction or the answer is not correct.")
#Restart
restart = input("Do you want to continue?(y/n):")
if restart == "y":
import time
print( "Restarting...")
time.sleep(1)
continue
if restart == "n":
import time
print( "Goodbye!")
time.sleep(10)
break
else:
import time
print( "Input Error!")
print( "Restarting...")
time.sleep(1)
continue
if kor == "n":
if D > 0:
import math
xone = (-b + D) / (2 * a)
xtwo = (-b - D) / (2 * a)
print ( "Discriminant = " + str(D))
print ( "x1 = " + str(xone))
print ( "x2 = " + str(xtwo))
print ( "if there are many numbers after the point then the answer is either a fraction or the answer is not correct.")
print ( "Try selecting a discriminant that is derived from the root!")
if D == 0:
import math
x = -b / (2 * a)
print ( "Discriminant = " + str(D))
print ( "x = " + str(x))
print ( "if there are many numbers after the point then the answer is either a fraction or the answer is not correct.")
if D < 0:
import math
print ( "Discriminant = " + str(D))
print ( "no roots! " )
print ( "if there are many numbers after the point then the answer is either a fraction or the answer is not correct.")
#Restart
restart = input("Do you want to continue?(y/n):")
if restart == "y":
import time
print( "Restarting...")
time.sleep(1)
continue
if restart == "n":
import time
print( "Goodbye!")
time.sleep(10)
break
else:
import time
print( "Input Error!")
print( "Restarting...")
time.sleep(1)
continue
else:
import time
print( "Input Error!")
print( "Restarting...")
time.sleep(1)
continue
else:
(it`s language part)
import time
print( "Input Error!")
print( "Restarting...")
time.sleep(1)
continue
此代码有很多重复。如果避免使用double while循环,而改用函数,则处理用户输入用例的所有可能组合会容易得多。这是我进行重组的动力。
import time, math
def f():
kor = input( "The discriminant is derived from the root?(y/n):")
if kor not in {'y', 'n'}:
print( "Input Error!")
print( "Restarting...")
time.sleep(1)
return f()
#a,b,c
try:
a = float( input( "Enter a number a:"))
b = float( input("Enter a number b:"))
c = float( input( "Enter a number c:"))
except (ValueError):
print ("Input error! This is not a number")
print ("Restarting...")
time.sleep(1)
return f()
D = (b * b) - 4 * a * c
return kor, a, b, c, D
while True:
language = input("1-Russian, 2-English, 3-UA? (Without spaces):")
if language == "2":
break
while language == "2":
kor, a, b, d, D = f()
if D > 0:
if kor == "y":
xone = (-b + (math.sqrt(D))) / (2 * a)
xtwo = (-b - (math.sqrt(D))) / (2 * a)
elif kor == "n":
print ( "Discriminant = " + str(D))
print ( "x1 = " + str(xone))
print ( "x2 = " + str(xtwo))
print ( "If there are many numbers after the point then the answer is either a fraction or the answer is not correct.")
elif D == 0:
x = -b / (2 * a)
print ( "Disctiminant = " + str(D))
print ( "x = " + str(x))
print ( "If there are many numbers after the point then the answer is either a fraction or the answer is not correct.")
elif D < 0:
print ( "Discriminant = " + str(D))
print ( "No roots! " )
print ( "If there are many numbers after the point then the answer is either a fraction or the answer is not correct.")
restart = input("Do you want to continue?(y/n):")
if restart == "y":
print( "Restarting...")
time.sleep(1)
elif restart == "n":
print( "Goodbye!")
time.sleep(10)
break
else:
print( "Input Error!")
print( "Restarting...")
time.sleep(1)