如何在 Python 中存储变量

How do I store a variable in Python

我试图让这段代码提出一个数学问题,并告诉用户如果答案是正确的,它应该加一个点并存储这些点以获得满分 10 分的结果,但如果他们答错了应该告诉他们正确答案并继续下一个问题,但不知道如何编码

number = 0
number = int(raw_input ("hello, please enter a times table you would like to practice "))    
while number > 12 or number < 2: 
    if number > 12:
        print "number is too big must be between 2 and 12 "
    else:
        print "number is too small must be between 2 and 12 "
    number = int(raw_input("please enter a number between 2 and 12: "))

import random
for i in range (11) :
    num_1 = random.randint(2, 12)
    right_answer = raw_input( str(i) + " what is " + str(number) + " times " + str(num_1))

这就是我要重建的东西

欢迎来到时代table测试者

您想 table 练习什么? 17

抱歉,太大了。数字介于 2 和 12 之间。

您想 table 练习什么? 0

抱歉,太小了。数字在 2 到 12 之间

您想 table 练习什么? 4

1 4 乘以 7 等于多少? 28 正确

2 4 乘以 6 等于多少? 24 正确

3 4 乘以 9 是多少? 36 正确

4 4 乘以 2 等于多少? 8 正确

5 4 乘以 5 是多少? 20 正确

6 4乘以3等于多少? 43 错误 4 * 3 = 12

7 4 乘以 4 是多少? 44 错误 4 * 4 = 16

8 4 乘以 6 等于多少? 46 错误 4 * 6 = 24

9 4 乘以 9 是多少? 49 错误 4 * 9 = 36

10 4 乘以 5 是多少? 45 错误 4 * 5 = 20

你得了 5 分(满分 10 分)

再见,感谢参与

这将数字 5 存储在变量中 points:

points = 5

或者,从技术上讲,它创建(或获取,如果已经存在)一个值为 5 的整数对象,并将标签 points 附加到它。但这是最接近在 Python 中获得的变量中存储值的方法。例如,Python 没有与 C 具有相同意义的变量。如果您的教科书采用 C 类型变量处理,这可能会造成混淆。

这是一个简单的方法:

import random

score = 0
number = int(raw_input ('hello, please enter a times table you would like to practice \n'))    
while number > 12 or number < 2: 
    if number > 12:
        print 'number is too big must be between 2 and 12 \n'
    else:
        print 'number is too small must be between 2 and 12 \n'
    number = int(raw_input('please enter a number between 2 and 12: \n'))

for i in range (11):

    num_1 = random.randint(2, 12)
    answer = int(raw_input('%s what is %s times %s ?\n)'%(i,number,num_1)))

    if answer == number*num_1:
        print 'Right answer!'
        score += 1
    else:
        print 'Wrong answer!\nThe correct answer was %s'%(number*num_1)

print 'You made a score of %s'%score
  1. 从用户处获取号码。检查输入字符串是否为not的个数,在2到12之间,如果不是告诉用户重新输入。
  2. 定义过程中需要的变量。使用 Counter 计算正确和无效的答案。
  3. 向用户提问并得到用户的回答。
  4. 检查给出的答案是否正确。
  5. 根据答案更新 Counter 个值。
  6. 打印结果。

代码:

print "Welcome to the times table tester."
#- Get Number from User.
while 1:
    try:
        number = int(raw_input ("What table would you like to practice?:"))
    except:
        print "You enter wrong string number. Only digits. Try again."
        continue

    if number>12:
        print "number is too big must be between 2 and 12 "
    elif number<2:
        print "number is too small must be between 2 and 12 "
    else:
        break

#- Define lib and variables.
from collections import Counter
cnt = Counter() 
import random
total_que =  10
tmp = "\n%s:What is %s times %s:"
tmp1 = "Incorrect.%s * %s = %s"

#-start Questions.
for i in xrange (1, total_que+1) :
    num_1 = random.randint(2, 12)
    answer_u = raw_input(tmp%(i, number, num_1))
    answer_a = number*num_1
    try:
        answer_u = int(answer_u)
    except:
        cnt["Incorrect"] += 1
        print tmp1%(number, num_1, answer_a)
        continue

    if answer_u==answer_a:
        cnt["correct"] += 1
        print "Correct"
    else:
        cnt["Incorrect"] += 1
        print tmp1%(number, num_1, answer_a)

#- Result.
print "=========Result:======"
print "You scored %s out of %s \ngoodbye and thanks for playing."%(cnt["correct"], total_que)

输出:

python test.py 
Welcome to the times table tester.
What table would you like to practice?:ww
You enter wrong string number. Only digits. Try again.
What table would you like to practice?:100
number is too big must be between 2 and 12 
What table would you like to practice?:1
number is too small must be between 2 and 12 
What table would you like to practice?:3

1:What is 3 times 6:18
Correct

2:What is 3 times 4:12
Correct

3:What is 3 times 5:15
Correct

4:What is 3 times 7:21
Correct

5:What is 3 times 9:27
Correct

6:What is 3 times 7:34
Incorrect.3 * 7 = 21

7:What is 3 times 12:123
Incorrect.3 * 12 = 36

8:What is 3 times 10:r
Incorrect.3 * 10 = 30

9:What is 3 times 12:36
Correct

10:What is 3 times 4:12
Correct
=========Result:======
You scored 7 out of 10 
goodbye and thanks for playing.

本程序使用while-else循环进行重复,直到找到一个介于2和12之间的数字,以及使用后弹出数组元素的逻辑。

import random
print "Welcome to the times table tester\n"
number = int(raw_input( "What table would you like to practice? "))

while (number > 12) or (number < 2):
    if number > 12 :
        print "\nSorry, thats too big. The number must be between 2 and 12"
        number = int(raw_input( "Enter some another number "))
    else :
        print "\nsorry, thats too small. The number must be between 2 and 12"
        number = int(raw_input( "Enter some another number "))
else:
    print "success"
    a=range(1,11)
    points = 0
    for i in range(1,11):
      rand_index = random.randint(0,len(a)-1)
      ans = int(raw_input("\n"+str(i)+": What is "+str(number)+" times "+str(a[rand_index])+"? "))
      if ans == number * a[rand_index]:
          print "Correct"
          a.pop(rand_index)
          points = points + 1
      else :
          print  "Incorrect "+str(number)+" * "+str(a[rand_index])+" = "+str(number*a[rand_index])
          a.pop(rand_index)
    print "You scored "+str(points)+" out of 10"      

输出:

Welcome to the times table tester

What table would you like to practice? 15

Sorry, thats too big. The number must be between 2 and 12
Enter some another number 1

sorry, thats too small. The number must be between 2 and 12
Enter some another number 7
success

1: What is 7 times 2? 14
Correct

2: What is 7 times 9? 63
Correct

3: What is 7 times 6? 36
Incorrect 7 * 6 = 42

4: What is 7 times 5? 35
Correct

5: What is 7 times 3? 21
Correct

6: What is 7 times 4? 28
Correct

7: What is 7 times 7? 40
Incorrect 7 * 7 = 49

8: What is 7 times 8? 56
Correct

9: What is 7 times 1? 7
Correct

10: What is 7 times 10? 70
Correct
You scored 8 out of 10