我如何在不生成退出代码 0 的情况下获得重复自身的功能?
How do i get a function to repeat its self without generating an exit code 0?
我用一个for-in loop
做了一个简单的骰子滚子,需要2个input
,骰子的数量和骰子的面数。完成滚动后,结果将打印到屏幕上。
完成初始滚动后,我想让代码重新开始,使用新的 inputs
,而不生成 exit code 0
。
##Original design obviously does not loop
from random import randint
import time
dice = int(input("\nHow many dice?\n "))
sides = int(input("How many sides?\n "))
def roll():
for quantity in range(dice):
time.sleep(.5)
print(randint(1, sides))
roll()
我将 input
变量从 global scope
移到了 local scope
,这样它们就可以在每次重复时更新。当通过调用 function
结束 function
时,我取得了一些成功。我能够无休止地 loop
这个过程而不会生成 exit code 0
但是超出 1
的任何数量的骰子似乎都没有注册。这样我一次只能掷一个骰子。
##Design 2 does not generate any exit codes and continues to loop
##endlessly but will not register more than 1 dice
from random import randint
import time
def roll():
dice = int(input("\nHow many dice?\n "))
sides = int(input("How many sides?\n "))
for quantity in range(dice):
time.sleep(.5)
print(randint(1, sides))
roll()
roll()
我已经更改了 function
,因此它不会以 calling
本身结束。相反,我创建了一个新函数,它只调用第一个函数。我在第一个函数之后放置了冗余函数,并且已经能够成功地实现我的目标,即随心所欲地滚动任意数量的单面骰子......两次。在两个 iterations/cycles 之后生成一个 exit code 0
##Current design will register all input data correctly
##but only loops 2 times before generating an `exit code 0`
from random import randint
import time
def roll():
dice = int(input("\nHow many dice?\n "))
sides = int(input("How many sides?\n "))
for quantity in range(dice):
time.sleep(.5)
print(randint(1, sides))
def roll_again():
roll()
roll()
roll_again()
有人知道我做错了什么吗?
您可以使用 while
循环
from random import randint
import time
def roll():
dice = int(input("\nHow many dice?\n "))
sides = int(input("How many sides?\n "))
for quantity in range(dice):
time.sleep(.5)
print(randint(1, sides))
while True:
roll()
这将 运行 脚本永远存在,所有滚动后都会有一个新的输入。
要退出,请按 Ctrl+C
背景:while
循环将在开始时检查条件,如果满足条件 运行 (True
)。在这种情况下,条件总是设置为 True
,因此它永远 满足 ;-)
您需要将游戏逻辑放在一个 while 循环中;玩完后输入零个骰子退出。
from random import randint
import time
def roll():
for quantity in range(dice):
time.sleep(.5)
print(randint(1, sides))
while True:
dice = int(input("\nHow many dice? (zero to stop)\n "))
if dice == 0:
break
sides = int(input("How many sides?\n "))
roll()
我用一个for-in loop
做了一个简单的骰子滚子,需要2个input
,骰子的数量和骰子的面数。完成滚动后,结果将打印到屏幕上。
完成初始滚动后,我想让代码重新开始,使用新的 inputs
,而不生成 exit code 0
。
##Original design obviously does not loop
from random import randint
import time
dice = int(input("\nHow many dice?\n "))
sides = int(input("How many sides?\n "))
def roll():
for quantity in range(dice):
time.sleep(.5)
print(randint(1, sides))
roll()
我将 input
变量从 global scope
移到了 local scope
,这样它们就可以在每次重复时更新。当通过调用 function
结束 function
时,我取得了一些成功。我能够无休止地 loop
这个过程而不会生成 exit code 0
但是超出 1
的任何数量的骰子似乎都没有注册。这样我一次只能掷一个骰子。
##Design 2 does not generate any exit codes and continues to loop
##endlessly but will not register more than 1 dice
from random import randint
import time
def roll():
dice = int(input("\nHow many dice?\n "))
sides = int(input("How many sides?\n "))
for quantity in range(dice):
time.sleep(.5)
print(randint(1, sides))
roll()
roll()
我已经更改了 function
,因此它不会以 calling
本身结束。相反,我创建了一个新函数,它只调用第一个函数。我在第一个函数之后放置了冗余函数,并且已经能够成功地实现我的目标,即随心所欲地滚动任意数量的单面骰子......两次。在两个 iterations/cycles 之后生成一个 exit code 0
##Current design will register all input data correctly
##but only loops 2 times before generating an `exit code 0`
from random import randint
import time
def roll():
dice = int(input("\nHow many dice?\n "))
sides = int(input("How many sides?\n "))
for quantity in range(dice):
time.sleep(.5)
print(randint(1, sides))
def roll_again():
roll()
roll()
roll_again()
有人知道我做错了什么吗?
您可以使用 while
循环
from random import randint
import time
def roll():
dice = int(input("\nHow many dice?\n "))
sides = int(input("How many sides?\n "))
for quantity in range(dice):
time.sleep(.5)
print(randint(1, sides))
while True:
roll()
这将 运行 脚本永远存在,所有滚动后都会有一个新的输入。
要退出,请按 Ctrl+C
背景:while
循环将在开始时检查条件,如果满足条件 运行 (True
)。在这种情况下,条件总是设置为 True
,因此它永远 满足 ;-)
您需要将游戏逻辑放在一个 while 循环中;玩完后输入零个骰子退出。
from random import randint
import time
def roll():
for quantity in range(dice):
time.sleep(.5)
print(randint(1, sides))
while True:
dice = int(input("\nHow many dice? (zero to stop)\n "))
if dice == 0:
break
sides = int(input("How many sides?\n "))
roll()