如何计算用户键入错误答案的次数(Python)

时间:2016-12-29 14:40:55

标签: python

我是python的新手,soo,是的。 基本上我有这个:

import time
import sys

def delay_print(s):
    for c in s:
        sys.stdout.write( '%s' % c )
        sys.stdout.flush()
        time.sleep(0.10)

name=input("What's your name?")
question1=input("How many centimeters are one meter?")
if question1=="100":
print("Correct!")
question2=input("How many meters in a centimeter?")
if question2=="0.001": 
    print("Correct!") 
import os 
time.sleep(1)
os.system('cls') 
time.sleep(1)
delay_print("Calculating results...")
time.sleep(3)
print("Good job,", name, "!")

我在问问题:确切地说是2。 所以我想跟踪用户错误提问的次数,所以最后我可以显示有多少问题是正确的,有多少是错的。 然后我想显示一条特定的消息: 因为当他们都是对的时候,我已经输入了那个。 当一个是正确的而一个错误我要输入: "不错(名字),但要小心!" 最后但并非最不重要的是,当他/她弄错了: "噢,伙计!那是非常糟糕的(名字)!要小心!"

1 个答案:

答案 0 :(得分:1)

只需简单地将else语句添加到if语句中即可处理程序在答案错误时应该执行的操作以及错误计数。最后根据print语句进行操作关于用户使用另一个if语句所犯错误的数量:

import time
import sys
import os

def delay_print(s):
    for c in s:
        sys.stdout.write( '%s' % c )
        sys.stdout.flush()
        time.sleep(0.00)
    print('\n')    #new line for keeping it neat

count = 0#variable to store num of wrong answers
name=input("What's your name?")
question1=input("How many centimeters are one meter?")
if question1=="100":
    print("Correct!")
else:#if its wrong do this
    print("wrong!")
    count = count + 1#adds 1 if wrong
question2=input("How many meters in a centimeter?")
if question2=="0.001":
    print("Correct!")
else:#if its wrong do this
    print("wrong!")
    count = count + 1#adds 1 if wrong

time.sleep(1)
os.system('cls')
time.sleep(1)
delay_print("Calculating results...")
time.sleep(3)
if count == 0 : #if nothings wrong do this
    print("Good job,",name,"!")
elif count == 1:# if 1 one wrong do this
    print("Not bad,",name,",but be more careful!")
else:#else if everythings wrong do this
    print("Oh man! That is very bad,",name,",be more careful!")

输出:

What's your name?Food
How many centimeters are one meter?67
wrong!
How many meters in a centimeter?00.01
wrong!
Calculating results...

Oh man! That is very bad, Food ,be more careful!