Python while/try/if循环
我只是在学习python,想知道是否有更好的方法来编写代码,而不是在while循环中使用try/except和if/else。这是从学习编码的艰难的方式,我试图给用户3次机会输入一个数字,在第三次机会,它退出使用死亡函数。(这些评论是为了我自己)Python while/try/if循环,python,Python,我只是在学习python,想知道是否有更好的方法来编写代码,而不是在while循环中使用try/except和if/else。这是从学习编码的艰难的方式,我试图给用户3次机会输入一个数字,在第三次机会,它退出使用死亡函数。(这些评论是为了我自己) def gold_room(): 打印“这间屋子里满是金子,你要多少钱?” chance=0#他们有多少机会键入一个数字 如果正确:#继续跑 下一步=原始输入(“>”) 尝试: how_mouly=int(下一步)#尝试将输入转换为数字 中断#如果工作
def gold_room():
打印“这间屋子里满是金子,你要多少钱?”
chance=0#他们有多少机会键入一个数字
如果正确:#继续跑
下一步=原始输入(“>”)
尝试:
how_mouly=int(下一步)#尝试将输入转换为数字
中断#如果工作,则中断循环并跳到**
除了:#如果不起作用,那么
如果机会<2:#如果是第一次或第二次,让他们再试一次
机会+=1
打印“最好键入一个数字…”
否则:#否则退出
死亡(“伙计,学着打一个数字。”)
如果多少小于50:#**
打印“很好,你不贪婪,你赢了!”
出口(0)
其他:
死了(“你这个贪婪的混蛋!”)
def死亡(原因):
打印为什么,“再见!”
出口(0)
下面是使用递归的另一种方法:
def dead(why):
print why, "Good bye!"
exit(0)
def typenumber(attempts):
if attempts:
answer = raw_input('> ')
try:
int(answer)
except ValueError:
print "Better type a number..."
return typenumber(attempts -1)
else:
return True
if typenumber(3):
print 'you got it right'
else:
dead("Man, learn to type a number.")
> a
Better type a number...
> b
Better type a number...
> 3
you got it right
这是你提供的精简版,缺少了很多你喜欢的文本,但希望它能为你提供更多关于其他封装方法的见解,而不是硬编码你的值。而不是将你的
封装在while
循环中,我可能会将while循环移动到的内部,除了
,以使您的代码更有条理,更易于阅读,这是Python的前提。下面是一个可以复制并运行的工作示例:
def gold_room():
print "this room is full of gold. How much do you take?"
while True: #keep running
next = raw_input("> ")
try:
how_much = int(next) #try to convert input to number
break #if works break out of loop and skip to **
except: #if doesn't work then
while True:
chance = 0 #how many chances they have to type a number
if chance < 2: #if first, or second time let them try again
chance += 1
print "Better type a number..."
else: #otherwise quit
dead("Man, learn to type a number.")
if how_much < 50: #**
print "Nice, you're not greedy, you win!"
exit(0)
else:
dead("You greedy bastard!")
def dead(why):
print why, "Good bye!"
exit(0)
gold_room()
def gold_room():
打印“这间屋子里满是金子,你要多少钱?”
如果正确:#继续跑
下一步=原始输入(“>”)
尝试:
how_mouly=int(下一步)#尝试将输入转换为数字
中断#如果工作,则中断循环并跳到**
除了:#如果不起作用,那么
尽管如此:
chance=0#他们有多少机会键入一个数字
如果机会<2:#如果是第一次或第二次,让他们再试一次
机会+=1
打印“最好键入一个数字…”
否则:#否则退出
死亡(“伙计,学着打一个数字。”)
如果多少小于50:#**
打印“很好,你不贪婪,你赢了!”
出口(0)
其他:
死了(“你这个贪婪的混蛋!”)
def死亡(原因):
打印为什么,“再见!”
出口(0)
黄金屋
假设代码正常工作,这是一个更好的问题。在程序中使用exit()是一种不好的做法。例如,这意味着如果不关闭较大的程序,就不能将代码嵌入较大的程序中。不要退出,而是从方法返回。这不起作用。如果你不输入一个数字,你就会陷入一个“更好地输入一个数字…”的无限循环。
def gold_room():
print "this room is full of gold. How much do you take?"
while True: #keep running
next = raw_input("> ")
try:
how_much = int(next) #try to convert input to number
break #if works break out of loop and skip to **
except: #if doesn't work then
while True:
chance = 0 #how many chances they have to type a number
if chance < 2: #if first, or second time let them try again
chance += 1
print "Better type a number..."
else: #otherwise quit
dead("Man, learn to type a number.")
if how_much < 50: #**
print "Nice, you're not greedy, you win!"
exit(0)
else:
dead("You greedy bastard!")
def dead(why):
print why, "Good bye!"
exit(0)
gold_room()