Python 有人知道我怎样才能把这个循环起来吗?
我需要通过将其放入循环来缩短代码。然而,当我尝试时,我不能在每一段中保留问题1、2、3、4、5。它是随机生成2个数字5次,用于小时间表测试(我学校的Python作业)。最简单的方法可能是在每次迭代时创建一个单词列表和索引。显然,当您希望能够扩展到任意数量时,这会成为一个问题,在这种情况下,您似乎可以使用所讨论的库之一。要显示单词,可以使用以下方法 试试这个:Python 有人知道我怎样才能把这个循环起来吗?,python,python-3.x,loops,Python,Python 3.x,Loops,我需要通过将其放入循环来缩短代码。然而,当我尝试时,我不能在每一段中保留问题1、2、3、4、5。它是随机生成2个数字5次,用于小时间表测试(我学校的Python作业)。最简单的方法可能是在每次迭代时创建一个单词列表和索引。显然,当您希望能够扩展到任意数量时,这会成为一个问题,在这种情况下,您似乎可以使用所讨论的库之一。要显示单词,可以使用以下方法 试试这个: import random number_words = ['one', 'two', 'three', 'four', 'five']
import random
number_words = ['one', 'two', 'three', 'four', 'five']
score = 0
for x in range(5):
print("Question {}".format(number_words[x]))
numberOne=random.randint(1,12)
numberTwo=random.randint(1,12)
question = "{} x {} = ".format(numberOne, numberTwo)
answer = int(input(question))
if answer == numberOne*numberTwo:
if x != 4:
print("Well Done It's Correct, Next Question")
else:
print("Well Done It's Correct, Thats The End")
score = score + 1
else:
if x != 4:
print("Better Luck Next Time, Next Question")
else:
print("Better Luck Next Time, Thats The End")
print("This is not in the loop")
# Write non-looping code here
随机导入
定义整数到整数(num):
# http://stackoverflow.com/questions/8982163/how-do-i-tell-python-to-convert-integers-into-words
#@无声之歌
d={0:'零',1:'一',2:'二',3:'三',4:'四',5:'五',
6:'六',7:'七',8:'八',9:'九',10:'十',,
11:'十一',12:'十二',13:'十三',14:'十四',,
15:'十五',16:'十六',17:'十七',18:'十八',,
19:'十九',20:'二十',
30:'三十',40:'四十',50:'五十',60:'六十',
70:'70',80:'80',90:'90'}
k=1000
m=k*1000
b=m*1000
t=b*1000
断言(0可能是@AriGold的重复项我不认为这是重复项。这个问题是为了在for
循环中正确打印问题编号而提出的。我如何将循环仅放在这一位代码上,它下面的代码(我没有显示)是包括在循环中,我不想它,我会添加在一个单独的评论,如果你想我的代码to@JarrethGriffith我做了两次编辑。首先,在循环的最后一次迭代中,您会收到一条消息说“到此结束”。循环仅适用于缩进到循环中的代码。例如,您可以看到我将打印出来(“这不在循环中”)
没有缩进-这将只打印一次,并且仅在循环完成5次后才会打印。这就是我放置其他代码的地方,但每次都在使用它,我将重试编辑-它已经工作了我没有正确复制代码,抱歉you@JarrethGriffith这是不可能的,只要代码从相同的位置开始作为for
中的f
,它不在for
循环的范围内,不会重复。你需要仔细检查你的缩进。是的,我知道了,这是因为我是pythonthank的新手,因为我是python的新手,我不懂你使用的一些命令。你能告诉我什么是“def”吗map、“lamba”和“%s”是fordef,它只是定义一个新函数的关键字,可以在代码的其他地方调用。lambda是一个匿名函数(您不必使用def,如果您只想定义一个函数使用一次,它会很有帮助),map可以使用该函数(第一个参数)并在列表的每个元素上调用它(第二个参数),“%s”仅用于字符串格式,感谢您帮助我更了解它@Steven Summers,thx,我将立即实施您的客观批评。这对未来的读者和我来说都是一件非常愉快的事情
import random
number_words = ['one', 'two', 'three', 'four', 'five']
score = 0
for x in range(5):
print("Question {}".format(number_words[x]))
numberOne=random.randint(1,12)
numberTwo=random.randint(1,12)
question = "{} x {} = ".format(numberOne, numberTwo)
answer = int(input(question))
if answer == numberOne*numberTwo:
if x != 4:
print("Well Done It's Correct, Next Question")
else:
print("Well Done It's Correct, Thats The End")
score = score + 1
else:
if x != 4:
print("Better Luck Next Time, Next Question")
else:
print("Better Luck Next Time, Thats The End")
print("This is not in the loop")
# Write non-looping code here
import random
print("welcome to the maths game")
score = 0
titles = map(lambda s: "Question %s" % s, ['One', 'Two', 'Three', 'Four', 'Five'])
def gen_randoms():
return random.randint(1, 12), random.randint(1, 12)
for t in titles:
print(t)
num_one, num_two = gen_randoms()
question = str(num_one) + " x " + str(num_two) + " = "
answer = int(input(question))
if answer == num_one * num_two:
print("Well Done It's Correct, Next Question")
score += 1 # puts the score plus one
else:
print("Better Luck Next Time, Next Question")
print("Your resulting score is %s" % score)
import random
def int_to_en(num):
# http://stackoverflow.com/questions/8982163/how-do-i-tell-python-to-convert-integers-into-words
# @SiLent SoNG
d = { 0 : 'zero', 1 : 'one', 2 : 'two', 3 : 'three', 4 : 'four', 5 : 'five',
6 : 'six', 7 : 'seven', 8 : 'eight', 9 : 'nine', 10 : 'ten',
11 : 'eleven', 12 : 'twelve', 13 : 'thirteen', 14 : 'fourteen',
15 : 'fifteen', 16 : 'sixteen', 17 : 'seventeen', 18 : 'eighteen',
19 : 'nineteen', 20 : 'twenty',
30 : 'thirty', 40 : 'forty', 50 : 'fifty', 60 : 'sixty',
70 : 'seventy', 80 : 'eighty', 90 : 'ninety' }
k = 1000
m = k * 1000
b = m * 1000
t = b * 1000
assert(0 <= num)
if (num < 20):
return d[num]
if (num < 100):
if num % 10 == 0: return d[num]
else: return d[num // 10 * 10] + '-' + d[num % 10]
if (num < k):
if num % 100 == 0: return d[num // 100] + ' hundred'
else: return d[num // 100] + ' hundred and ' + int_to_en(num % 100)
if (num < m):
if num % k == 0: return int_to_en(num // k) + ' thousand'
else: return int_to_en(num // k) + ' thousand, ' + int_to_en(num % k)
if (num < b):
if (num % m) == 0: return int_to_en(num // m) + ' million'
else: return int_to_en(num // m) + ' million, ' + int_to_en(num % m)
if (num < t):
if (num % b) == 0: return int_to_en(num // b) + ' billion'
else: return int_to_en(num // b) + ' billion, ' + int_to_en(num % b)
if (num % t == 0): return int_to_en(num // t) + ' trillion'
else: return int_to_en(num // t) + ' trillion, ' + int_to_en(num % t)
raise AssertionError('num is too large: %s' % str(num))
def get_question(num, *args):
# param: num, convert int to word (instead list = ['one', 'two', 'three', 'four', 'five'.......])
# your game should be scalable for N number of questions
q_header = 'Question {0}:'.format(int_to_en(num)) # convert and add it to string, use {n} with index, faster than without
r_N_a, r_N_b = [random.randint(1,12) for __ in range(2)] # to random num
q_str = '{0} x {1} = '.format(r_N_a, r_N_b) # creating question string
return (q_header, r_N_a*r_N_b, q_str) # return, you could now call get_question method from outside, if you need a question block in your other modules
def run_game(num, score = 0):
for n in range(num):
q_header, expectation, q_str = get_question(n)
print (q_header)
answer = int(input(q_str))
if not answer == expectation:
print ('wrong')
else:
score += 1
print ('next')
return score
score = run_game(3) # 3 questions
block = [get_question(n) for n in range(50)] # 50 question/solution block
print (block)
print (block[24])
[('Question zero:', 40, '5 x 8 = '), ('Question one:', 80, '8 x 10 = '), ('Question two:', 49, '7 x 7 = '), ('Question three:', 2, '1 x 2 = '), ('Question four:', 60, '12 x 5 = '), ('Question five:', 77, '7 x 11 = '), ('Question six:', 144, '12 x 12 = '), ('Question seven:', 77, '7 x 11 = '), ('Question eight:', 88, '8 x 11 = '), ('Question nine:', 5, '1 x 5 = '), ('Question ten:', 40, '5 x 8 = '), ('Question eleven:', 72, '12 x 6 = '), ('Question twelve:', 24, '4 x 6 = '), ('Question thirteen:', 72, '9 x 8 = '), ('Question fourteen:', 1, '1 x 1 = '), ('Question fifteen:', 2, '2 x 1 = '), ('Question sixteen:', 24, '6 x 4 = '), ('Question seventeen:', 80, '8 x 10 = '), ('Question eighteen:', 36, '4 x 9 = '), ('Question nineteen:', 99, '11 x 9 = '), ('Question twenty:', 42, '6 x 7 = '), ('Question twenty-one:', 12, '12 x 1 = '), ('Question twenty-two:', 96, '12 x 8 = '), ('Question twenty-three:', 50, '5 x 10 = '), ('Question twenty-four:', 72, '12 x 6 = '), ('Question twenty-five:', 80, '8 x 10 = '), ('Question twenty-six:', 24, '3 x 8 = '), ('Question twenty-seven:', 88, '8 x 11 = '), ('Question twenty-eight:', 108, '9 x 12 = '), ('Question twenty-nine:', 12, '3 x 4 = '), ('Question thirty:', 144, '12 x 12 = '), ('Question thirty-one:', 2, '2 x 1 = '), ('Question thirty-two:', 56, '8 x 7 = '), ('Question thirty-three:', 32, '8 x 4 = '), ('Question thirty-four:', 5, '1 x 5 = '), ('Question thirty-five:', 16, '4 x 4 = '), ('Question thirty-six:', 84, '7 x 12 = '), ('Question thirty-seven:', 44, '11 x 4 = '), ('Question thirty-eight:', 7, '7 x 1 = '), ('Question thirty-nine:', 54, '9 x 6 = '), ('Question forty:', 22, '2 x 11 = '), ('Question forty-one:', 77, '7 x 11 = '), ('Question forty-two:', 20, '5 x 4 = '), ('Question forty-three:', 9, '1 x 9 = '), ('Question forty-four:', 8, '2 x 4 = '), ('Question forty-five:', 24, '8 x 3 = '), ('Question forty-six:', 15, '5 x 3 = '), ('Question forty-seven:', 24, '8 x 3 = '), ('Question forty-eight:', 30, '6 x 5 = '), ('Question forty-nine:', 96, '12 x 8 = ')]
('Question twenty-four:', 110, '11 x 10 = ')