我也做了一个菜单。选择的主要问题是:
import random
points=0
score=0
math_score=0
correctage= False
print ('Please choose one of the following: \n 1.General Quiz \n 2.Maths quiz \n 3.Exit/Quit')
selection= input ('Choose what you want to do: ')
while selection:
menu = {}
menu['1']="General Quiz."
menu['2']="Maths Quiz."
menu['3']="Exit"
while not correctage:
if selection == '1':
print ('Hello I am a Super Computer Genius Ajin')
name= (input ( ' What is your Name?')).title()
print( ' So your name is ' + name)
try:
age= int(input( ' How old are you?'))
print( ' So your Age is ' +str( age))
if age==14 or age==15:
print('You can Enter')
points=points+10
print( name+ ', You got ' +str (points) + ' points')
q1=input('Who won the last world cup ').upper()
if q1=='GERMANY':
print('Well Done')
points=points+1
score=score+1
print( name+ ', You got ' +str (points) + ' points')
else:
print ('wrong')
points=points-0
print( name+ ', You got ' +str (points) + ' points')
q2=input('Who won the founder of apple ').upper()
if q2=='STEVE JOBS':
print('Well Done')
points=points+1
score=score+1
print( name+ ', You got ' +str (points) + ' points')
else:
print ('wrong')
points=points-0
print( name+ ', You got ' +str (points) + ' points')
q3=input('Which company jailbroke ios 9 ').upper()
if q3=='PANGU':
print('Well Done')
points=points+1
score=score+1
print( name+ ', You got ' +str (points) + ' points')
else:
print ('wrong')
points=points-0
print( name+ ', You got ' +str (points) + ' points')
q4=input('What is the name of the latest apple phone? ').upper()
if q4=='IPHONE 6S':
print('Well Done')
points=points+1
score=score+1
print( name+ ', You got ' +str (points) + ' points')
else:
print ('wrong')
points=points-0
print( name+ ', You got ' +str (points) + ' points')
q5=input('What is he capital of India? ').upper()
if q5=='NEW DELHI':
print('Well Done')
points=points+1
score=score+1
print( name+ ', You got ' +str (points) + ' points')
else:
print ('wrong')
points=points-0
print( name+ ', You got ' +str (points) + ' points')
q6=input('Who is the fastest man ? ').upper()
if q6=='USAIN BOLT'or q6=='MO FARAH':
print('Well Done')
points=points+1
score=score+1
print( name+ ', You got ' +str (points) + ' points')
else:
print ('wrong')
points=points-0
print( name+ ', You got ' +str (points) + ' points')
q7=input('What is Android? ').upper()
if q7=='OPERATING SYSTEM':
print('Well Done')
points=points+1
score=score+1
print( name+ ', You got ' +str (points) + ' points')
else:
print ('wrong')
points=points-0
print( name+ ', You got ' +str (points) + ' points')
q8=input('Who is Thomas Edison ? ').upper()
if q8=='INVENTOR OF THE LIGHT BULB' or q8=='INVENTOR OF LIGHT BULB':
print('Well Done')
points=points+1
score=score+1
print( name+ ', You got ' +str (points) + ' points')
else:
print ('wrong')
points=points-0
print( name+ ', You got ' +str (points) + ' points')
q9=input('What does WWW mean ? ').upper()
if q9=='WORLD WIDE WEB':
print('Well Done')
points=points+1
score=score+1
print( name+ ', You got ' +str (points) + ' points')
else:
print ('wrong')
points=points-0
print( name+ ', You got ' +str (points) + ' points')
q10=input('Latest windows operating system? ').upper()
if q10=='WINDOWS 10':
print('Well Done')
points=points+1
score=score+1
print( name+ ', You got ' +str (points) + ' points')
else:
print ('wrong')
points=points-0
print( name+ ', You got ' +str (points) + ' points')
print(" Thank you for playing ", name, "You have scored" , score, 'out of 10')
text_file= open ("general_quiz.txt", "a")
text_file.write (name )
text_file.write (" scored ")
text_file.write(str(score ))
text_file.write (' out of 10, ' )
text_file.close()
print ("previous people who have played and what they scored out of 10")
text_file = open("general_quiz.txt", "r")
print (text_file.read())
text_file.close()
break
else:
print (' you shall not pass')
correctage=False
break
except:
print('Not a Valid age')
break
elif selection =='2':
for i in range(10):
num1=random.randint(1,12)
num2=random.randint(1,12)
ops= ('+', '-', '*')
operation= random.choice(ops)
answer=1
if operation == '+':
answer == num1+num2
elif operation == '-':
answer == num1-num2
elif operation == '*':
answer == num1*num2
maths_name= (input ( ' What is your Name?')).title()
print( ' Hello ' + maths_name)
print ('What is ' + str(num1) + operation + str(num2))
user_answer= int(input('Enter answer: '))
if user_answer == answer:
print('correct')
math_score=math_score+1
else:
print ('Incorect')
math_score=math_score-0
print(" Thank you for playing ", maths_name, "You have scored" , math_score, 'out of 10')
text_file= open("maths_score.txt", "a")
text_file.write(maths_name )
text_file.write(' Scored ')
text_file.write (str(math_score ))
text_file.write(' Out of 10, ')
text_file.close()
print('Previous people who have played are:')
text_file= open("maths_score.txt", "r")
print(text_file.read())
text_file.close()
elif selection =='3':
break
else:
break
我的代码问题是数学测验没有正常工作,一般测验没有破坏所以请有人帮助我。
我花了一天时间试图解决问题
答案 0 :(得分:0)
错误在于:
if operation == '+':
answer == num1+num2
elif operation == '-':
answer == num1-num2
elif operation == '*':
answer == num1*num2
answer == num1+num2
形式的三行比较answer
(您最初设置为1
)是否等于num1+num2
,然后对结果无效比较。
我想你想要以下内容:
if operation == '+':
answer = num1+num2
elif operation == '-':
answer = num1-num2
elif operation == '*':
answer = num1*num2
您可能还想考虑移动程序的一部分,在for
循环之外询问您的姓名 - 此时它会在每个问题之前询问您的姓名。