【发布时间】:2014-09-23 08:36:24
【问题描述】:
我是 python 新手。这是我想在我的代码中做的一个小例子。我想保存正方形和圆形的值。 所以它会问“确实想改变值......”你按一个正方形,它从 0 到 1,然后它再次询问,你按 2,然后它再次上升到 2。 我不想在我的程序中使用全局变量。 我正在阅读帖子说无全局变量的答案是将变量传递给函数并进行更改然后返回它们。我认为这不适用于我的 while 循环。
loopcounter = 0
square = 0
circle = 0
def varibleChanges(circle, square):
#global circle, square
print 'Would you like to change circle or square?'
print '1. circle' '\n' '2. square'
choice = raw_input('>>')
if choice == '1':
square = square + 1
elif choice == '2':
circle = circle + 1
print 'square: ', square
print 'circle: ', circle
while loopcounter <=2:
print 'this is the begining of the loop'
varibleChanges(circle, square)
loopcounter +=1
print "this is the end of the loop\n"
将变量存储在代码之外工作,例如写入文件,(无论如何我都会有保存功能) 还是最好重新考虑一下代码?
【问题讨论】:
标签: python variables while-loop