python 乘法测验语法无效
python multiplication quiz invalid syntax
我有这个 python 乘法测验。
import random
score = 0
continue = True
while continue:
a = random.randint(1, 12)
b = random.randint(1, 12)
product = a * b
guess = int(input('What is '+str(a)+' times '+str(b)+'? (press "q" to quit):'))
if guess == 'q':
continue = False
if guess != product:
print('Sorry, this is wrong. It should be '+str(product)+'.')
continue = False
if guess == product:
print('Good job. You got it right.')
print('Thanks for playing! You scored '+str(score)+'.')
当我在 continue = True
.
行尝试 运行 时,它一直说 SyntaxError: invalid syntax
在添加 continue
查询之前,它运行良好:
import random
score = 0
while True:
a = random.randint(1, 12)
b = random.randint(1, 12)
product = a * b
guess = int(input('What is '+str(a)+' times '+str(b)+'? (press "q" to quit):'))
if guess == 'q':
break
if guess != product:
print('Sorry, this is wrong. It should be '+str(product)+'.')
if guess == product:
print('Good job. You got it right.')
print('Thanks for playing! You scored '+str(score)+'.')
我不确定 continue = True
这行有什么问题。据我所知,这是将 True
分配给变量 continue
。请帮忙!
continue
是一个 python 关键字,例如 if
、else
、True
、break
等,因此您可以给它赋值。如果您想使用此名称,请尝试 continue_
(来自 pep8)。
还有 continue_
修复输入 'q'
会导致异常,因为 python 尝试执行 int('q')
,但这是行不通的。
您没有在函数中增加分数,因此如果正确添加 score += 1
会有所帮助。
import random
score = 0
continue_ = True
while continue_:
a = random.randint(1, 12)
b = random.randint(1, 12)
product = a * b
guess = input('What is '+str(a)+' times '+str(b)+'? (press "q" to quit):')
if guess == 'q':
break
if int(guess) != product:
print('Sorry, this is wrong. It should be '+str(product)+'.')
continue_ = False
if int(guess) == product:
print('Good job. You got it right.')
score += 1
print('Thanks for playing! You scored '+str(score)+'.')
例如会输出
What is 6 times 7? (press "q" to quit):42
Good job. You got it right.
What is 10 times 7? (press "q" to quit):70
Good job. You got it right.
What is 8 times 7? (press "q" to quit):56
Good job. You got it right.
What is 9 times 10? (press "q" to quit):90
Good job. You got it right.
What is 2 times 12? (press "q" to quit):24
Good job. You got it right.
What is 12 times 3? (press "q" to quit):36
Good job. You got it right.
What is 11 times 6? (press "q" to quit):66
Good job. You got it right.
What is 11 times 7? (press "q" to quit):q
Thanks for playing! You scored 8.
我有这个 python 乘法测验。
import random
score = 0
continue = True
while continue:
a = random.randint(1, 12)
b = random.randint(1, 12)
product = a * b
guess = int(input('What is '+str(a)+' times '+str(b)+'? (press "q" to quit):'))
if guess == 'q':
continue = False
if guess != product:
print('Sorry, this is wrong. It should be '+str(product)+'.')
continue = False
if guess == product:
print('Good job. You got it right.')
print('Thanks for playing! You scored '+str(score)+'.')
当我在 continue = True
.
SyntaxError: invalid syntax
在添加 continue
查询之前,它运行良好:
import random
score = 0
while True:
a = random.randint(1, 12)
b = random.randint(1, 12)
product = a * b
guess = int(input('What is '+str(a)+' times '+str(b)+'? (press "q" to quit):'))
if guess == 'q':
break
if guess != product:
print('Sorry, this is wrong. It should be '+str(product)+'.')
if guess == product:
print('Good job. You got it right.')
print('Thanks for playing! You scored '+str(score)+'.')
我不确定 continue = True
这行有什么问题。据我所知,这是将 True
分配给变量 continue
。请帮忙!
continue
是一个 python 关键字,例如 if
、else
、True
、break
等,因此您可以给它赋值。如果您想使用此名称,请尝试 continue_
(来自 pep8)。
还有 continue_
修复输入 'q'
会导致异常,因为 python 尝试执行 int('q')
,但这是行不通的。
您没有在函数中增加分数,因此如果正确添加 score += 1
会有所帮助。
import random
score = 0
continue_ = True
while continue_:
a = random.randint(1, 12)
b = random.randint(1, 12)
product = a * b
guess = input('What is '+str(a)+' times '+str(b)+'? (press "q" to quit):')
if guess == 'q':
break
if int(guess) != product:
print('Sorry, this is wrong. It should be '+str(product)+'.')
continue_ = False
if int(guess) == product:
print('Good job. You got it right.')
score += 1
print('Thanks for playing! You scored '+str(score)+'.')
例如会输出
What is 6 times 7? (press "q" to quit):42
Good job. You got it right.
What is 10 times 7? (press "q" to quit):70
Good job. You got it right.
What is 8 times 7? (press "q" to quit):56
Good job. You got it right.
What is 9 times 10? (press "q" to quit):90
Good job. You got it right.
What is 2 times 12? (press "q" to quit):24
Good job. You got it right.
What is 12 times 3? (press "q" to quit):36
Good job. You got it right.
What is 11 times 6? (press "q" to quit):66
Good job. You got it right.
What is 11 times 7? (press "q" to quit):q
Thanks for playing! You scored 8.