如何在Python中存储变量

时间:2015-01-06 09:28:30

标签: python variables

我试图让这段代码问一个数学问题并告诉用户如果答案是正确的,它应该添加一个点并存储点以获得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))

这是我尝试重新创建的内容

欢迎来到时代表测试员

你想练习什么表? 17

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

你想练习什么表? 0

抱歉,那太小了。数字介于2到12之间

你想练习什么表? 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

你在10分中得到了5分

再见,谢谢你的发挥

4 个答案:

答案 0 :(得分:2)

这是一个简单的方法:

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 :(得分:0)

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

points = 5

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

答案 2 :(得分:0)

  1. 从用户获取号码。检查输入字符串是否为数字,在2到12之间,如果没有告诉用户再次输入。
  2. 定义过程中所需的变量。使用Counter来计算正确无效的答案。
  3. 向用户提问并从用户那里得到答案。
  4. 检查给定答案是否正确。
  5. 根据回答更新Counter值。
  6. 打印结果。
  7. 代码:

    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.
    

答案 3 :(得分:0)

这个程序使用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