python测验验证不起作用

时间:2015-08-26 13:25:53

标签: python

验证不起作用。我不知道为什么,有没有办法验证字符串。提出的问题是无穷无尽的,我需要提出10个问题

import random

name=(input("Please enter your name"))
print("welcome",name,"the arithmetic is about to start")

question=0


while question<10:
    number=random.randint(1,10)
    numbers=random.randint(1,10)
    arith=random.choice("+" "-" "/")

if arith=="+":
    print(number,arith,numbers)
    answer=number+numbers





if arith=="-":
    print(number,arith,numbers)
    answer=number-numbers



if arith=="/":
    print(number,arith,numbers)
    answer=number/numbers



while True:
        try:     
            usersanswer= int(input())
        except ValueError:
            print ("That is not a valid answer")
            continue
        if usersanswer==answer:
            print("correct")
            break
    else:
        print("incorrct")

验证不起作用。我不知道为什么,有没有办法验证字符串

2 个答案:

答案 0 :(得分:1)

在第三行中,您要求input。但名称是一个字符串,因此您需要raw_inputraw_input采用字符串,input只接受数值。 Python 2.7 getting user input and manipulating as string without quotations

您的代码中没有任何地方更新变量questions,我猜这是一个计数器。每当询问问题时,您都必须使用question += 1更新。

最后,你最后的代码真的没有意义。根据代码,它会检查它是否是一个字符串,然后将其与答案进行比较。 if语句必须在try范围内。

else语句与任何外部缩进都不匹配。

最后,由于while True:你的代码将永远不会退出循环,除非答案是错误的。在整个程序终止时。我看到你要编写什么样的程序,但随机数生成的参数必须在某种while question <= 10循环内。截至目前,程序中只有两行受到第一个while循环的影响。

编辑:我正在研究一个很好的示例代码。希望这个答案有助于我完成它。 编辑:这是代码,显示它在while循环中的工作方式。

import random
from random import randint

name = raw_input("Hi, what is your name?\n") # Asks for name
print "Hi " +name+ " let's get started!"

score_count = 0
question_count = 0 # creates counter
while question_count <= 10: # Everything MUST BE WITHIN THIS LOOP
    # makes numbers and operator
    first_number = randint(1,10)
    second_number = randint(1,10)
    oper = random.choice("+""-""*")
    # determines the problem
    if oper == "+":
        answer = first_number + second_number
        print first_number,second_number,oper
    elif oper == "-":
        answer = first_number - second_number
        print first_number,second_number,oper
    elif oper == "*":
        answer = first_number*second_number
        print first_number, second_number, oper
    user_answer = int(raw_input("Your answer: "))
    if user_answer != answer: 
        print 'Wrong answer! try again!'
        user_answer = int(raw_input('Your answer: '))
    if user_answer == answer: # exits the while loop when the correct answer is given
        if question_count < 10:
            print 'Well done! Now onto question number {0}'.format(question_count+1)
            score_count += 1
        elif question_count == 10:
            print 'Well done! You are done!'
            score_count += 1
    else:
        print 'Something is wrong.'
    question_count += 1 # updates the variable
    # GOES BACK TO THE BEGINNING UNTIL question_count IS GREATER THAN OR EQUAL TO 10
print "Your score was: {}".format(score_count)

快乐的编码!祝你好运!

答案 1 :(得分:0)

嗨,我是内森,我看到这篇文章我迟到了 5 年,但我想如果这里有人知道 python 我有一个更容易(在我看来)在 python 3 中做到这一点的方法,代码如下:

import random #random module automatically downloaded when you install python

name = input("Please enter your name ")
print("welcome",name,"the arithmetic is about to start")

question=0
while question<10:
    number=random.randint(1,10) #creating a random number
    numbers=random.randint(1,10) #creating a random number
    list = ["+","-","/"] #creating a list (or sometimes called array)
    arith=random.choice(list) #getting random operators from list (+,-,/)
    question += 1 #basically means add one to question variable each time in loop
    if arith=="+":
        print(number,arith,numbers)
        answer=number+numbers
        
        
    elif arith=="-":
        print(number,arith,numbers)
        answer=number-numbers
        
        
    elif arith=="/":
        print(number,arith,numbers)
        answer=number/numbers
        answer = int(answer)
        
    #from HERE
    useranswer = "initialising this variable"
    while useranswer == "initialising this variable":
        try:
            usersanswer= int(input())
            if usersanswer==answer:
                print("correct")
                break
            else:
                print("incorrect")
        except ValueError:
            print ("That is not a valid answer")
    #to HERE it is input validation this takes a while to explain in just commenting
    #but if you dont know what this is then copy this link https://youtu.be/EG69-5U2AfU
    #and paste into google for a detailed video !!!!!!

我希望这会有所帮助,这是一个更简化的注释代码,可以帮助您在 Python 中编写代码