Python 3.x 在Python3.6中,如何创建变量的副本来为其分配不同的值?
我对python相当陌生,使用的是3.6,有点卡住了。我的节目-Python 3.x 在Python3.6中,如何创建变量的副本来为其分配不同的值?,python-3.x,Python 3.x,我对python相当陌生,使用的是3.6,有点卡住了。我的节目- import time asdm = input ('what operation is it? ') #tell the program which operation while True: #list is in the program if asdm == 'idk': #if the a
import time
asdm = input ('what operation is it? ') #tell the program which operation
while True: #list is in the program
if asdm == 'idk': #if the answer of the question
print('* = product of') #is 'idk', list these options
print('/ = quotient of')
print('+ = sum of')
print('- = subtract')
print('** = to the power of')
print('// = floored quotient of')
print('% = remainder of')
print('')
time.sleep(1)
asdm = old_asdm
asdm = input('what operation is it? ') #ask the same question
if not 'idk': #if the answer is one of the listed
break #end the loop
time.sleep(3)
while True:
print ('what are the variables?')
a = input ('what is the value of a? ') #value 'a'
b = input ('what is the value of b? ') #value 'b'
if asdm == '*': #if multiplication
print ('a * b')
print (a * b) #print 'a*b' and the answer
if asdm == '/': #if division
print ('a / b')
print (a / b) #print 'a/b' and the answer
if asdm == '+': #if addition
print ('a + b')
print (a + b) #print 'a+b' and the answer
if asdm == '-': #if subtraction
print ('a - b')
print (a - b) #print 'a-b' and the answer
if asdm == '**': #if power
print ('a ** b')
print (a ** b) #print 'a**b' and the answer
if asdm == '//': #is division with no remainder`
print ('a // b')
print (a // b) #print 'a//b' and the answer
time.sleep(4)
input('another equation? ')
if 'no':
break
time.sleep(3) #end
input ('Leave?')
- 如何制作asdm的副本,以使用该副本代替分配的
? 我有其他一切正常工作(大概),但这一点我不明白asdm
- 不完全确定您想要实现什么,但我认为这应该可行
我将您的输入放在第一个while循环中。这样,如果asdm变量在代码的下一部分中不是“idk”(而不是再次询问),则可以简单地重用asdm变量。在您的代码中,您尝试使用语句
asdm=old\u asdm
,其中old\u asdm尚未定义,这应该是错误的。此外,您的检查如果不是“idk”
则无效。您需要检查asdm!='idk’。
最后,在所有输入完成后,您尝试对字符串进行数学运算,因此我在输入上使用float()将字符串转换为数字。最后的另一个等式输入条件语句也包含与上面类似的错误
import time
while True: #list is in the program
asdm = input ('what operation is it? ')
if asdm == 'idk': #if the answer of the question
print('* = product of') #is 'idk', list these options
print('/ = quotient of')
print('+ = sum of')
print('- = subtract')
print('** = to the power of')
print('// = floored quotient of')
print('% = remainder of')
print('')
time.sleep(1)
if asdm != 'idk':: #if the answer is one of the listed
break #end the loop
time.sleep(3)
while True:
print ('what are the variables?')
a = float(input('what is the value of a? ')) #value 'a'
b = float(input('what is the value of b? ')) #value 'b'
if asdm == '*': #if multiplication
print ('a * b')
print (a * b) #print 'a*b' and the answer
if asdm == '/': #if division
print ('a / b')
print (a / b) #print 'a/b' and the answer
if asdm == '+': #if addition
print ('a + b')
print (a + b) #print 'a+b' and the answer
if asdm == '-': #if subtraction
print ('a - b')
print (a - b) #print 'a-b' and the answer
if asdm == '**': #if power
print ('a ** b')
print (a ** b) #print 'a**b' and the answer
if asdm == '//': #is division with no remainder`
print ('a // b')
print (a // b) #print 'a//b' and the answer
time.sleep(4)
another_equation = input('another equation? ')
if another_equation == 'no':
break
time.sleep(3) #end
input ('Leave?')
发布您收到的错误使我们可以更轻松地帮助您。您可以尝试将代码缩减为显示相同问题的较小版本吗?看,当我运行程序时,它一直认为'asdm'的值是'idk',所以我无法跳出循环。代码运行得更好。非常感谢你。今天就要交了,看来真的很有帮助,谢谢你。我刚给你做作业了吗?o、 哦,希望你能拿到A!;)有点像,但那只是第二个障碍。需要为初学者编写一个超级复杂的代码,或者是这个代码,或者是一个猜测数字的代码。我认为我不能用猜测代码做很多事情,对于arduino汽车来说,我不能用其他代码做更多的事情,所以剩下的就是这个。我现在正在做一个保存的工作,这样在最后我可以输入一个输入,询问用户是否想做另一个等式,然后它就转到了起点。我想你不知道怎么做,是吗?哦,我得到了班上一个孩子乔丹的帮助。他是个十足的技术迷,所以python、c#、c+等等,他都喜欢。因此,如果我使iasdm=None',那么它将重置asdm,然后出现中断问题。所以我再次提出这个问题,然后“如果asdm!=”idk':break',它可以工作。我不知道为什么,但它似乎不是从我用你的代码替换我的代码时复制过来的,所以我认为它是正确的(除了双冒号)“我目前正在进行保存,以便在最后输入一个输入,询问用户是否想做另一个等式,然后它进入起点。我想你不知道该怎么做,是吗?“我会使用while True循环,并以if输入(“另一个等式?(yes/no”)=“no”:break结尾。我不确定你的第二条评论是什么意思。