我有一些简单的代码,可以通过执行 y2-y1/x2-x1 来找到 m 的值。但是,由于某种原因, 的值x1
被 覆盖x2
。这很奇怪,因为y1
和不会发生同样的情况y2
。
这是代码:
def asker():
global y2
global y1
global x1
global x2
y2 = float(input("What's your y2 value?:"))
y1 = float(input("What is your y1 value?"))
x2 = float(input("What is your x2 value?:"))
x1 = float(input("What is your x1 value?:"))
#Lots of coding until:
elif equation == 'point':
while True:
slope = input("Do you need to find slope? Type 'yes' or 'no'.")
if slope == 'yes':
asker()
m = y2-y1/x2-x1
stringer()
break
elif slope == 'no':
m = input("What is the slope?")
y1 = float(input("What is your y1 value?"))
x1 = float(input("What is your x1 value?:"))
break
else:
print("Must enter in either 'yes' or 'no'")
continue
m,y1,x1 = str(m),str(y1),str(x1)
print("Your equation is: y - {} = {}(x - {})".format(y1,m,x1))
我打印出该if slope == 'yes'
部分中的所有值,并且所有值都是正确的。我在 if/elif/else 语句之外做了同样的事情,并且除了某些原因 x2 覆盖了 x1 之外,所有以前的值都被保留了下来。
如果您需要,这里是我整个文件的链接:https : //repl.it/repls/KeyScientificGraduate
查看 stringer() 函数中作业的左侧。
y1,x1,y2,x1,m = str(y1),str(x1),str(y2),str(x2),str(m)
看起来您从解压缩的元组中分配了两次 x1 。
这说明了为什么使用全局变量是不好的(大多数时候)。很难说事情在哪里得到更新。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句