python-3.x - 我有中断程序的问题
问题描述
我有一个问题,当我在启动“#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
解决方案
这段代码有很多重复。如果您避免双 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)
推荐阅读
- rx-java - 为什么在 RxJava 中使用 onBackpressureDrop () 会发生这种情况
- java - Selenium WebDriver / Java - 模拟人类光标移动
- java - Apache Ignite 2.x - 我可以利用堆外存储并且仍然有基于时间的驱逐(来自堆上存储和堆外存储)吗?
- dependencies - Android Studio 中的远程二进制文件是什么?幕后究竟做了什么?
- c# - ActionResult 返回时缺少 Null 属性
- python - Python tz 感知 str 到 datetime 对象以进行重采样计算
- sql - 仅针对特定值忽略 WHERE 子句
- ios - 如何在多个 iOS 模拟器上同时运行 Flutter 应用?
- angular - 将单击事件从一个子组件传递到另一个子组件
- ios - 如何在 SwiftUI 中应用大于底层视图的叠加层