2016-12-29 134 views
-1

我对python,soo,yeah来说很新。 基本上我有这样的:如何计算用户输入错误答案的次数(Python)

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. 所以我想跟踪多少次用户已经有一个问题错了,所以最后我可以显示如何许多问题都是正确的,有多少错误。 然后我想显示一个特定的消息: 因为当他们都是正确的,我已经输入的。 当一个是正确的,一个错误我想输入: “不错(名称),但要更加小心!” 最后但并非最不重要的一点,当他/她得到这一切错误: “哦,人!这是非常糟糕的(名字)!要更加小心!

+2

有一些变量'number_correct = 0',然后添加一个到它'number_correct + = 1'每次打印时'correct' –

+1

使用'else'和一个计数器变量? – UnholySheep

+1

一厘米多少米不是== 0.001 – Philipp

回答

1

只需简单地添加else语句,你if语句来处理你的程序应该做的事情,如果答案是错误的,如果是错的计数it.Lastly做出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!