请问我能否对我的代码提供一些帮助,因为该功能不适用于NumberError()
关键是,如果在回答数学测验时你错误地键入了一个字母,它会说&#39 ;请输入数字'但是我不能让它工作,因为错误不断出现:
Traceback (most recent call last):
File "C:/Users/Helen/Documents/Computer Science Summer Task/Helen_Summer Tasks-1_2_3.py", line 77, in <module>
StudentAnswer = int(input("Please answer the question: "))
ValueError: invalid literal for int() with base 10: 'g'
以下是我到目前为止的尝试:
def NumberError():
if StudentAnswer not in Turns '1,2,3,4,5,6,7,8,9,10':
print('Please enter a NUMBER.')
else:
return ("\nQuestion")
访问测验的密码选项也不能完全发挥作用:到目前为止,这是我所拥有的内容,如果能得到一些我需要做的正确以及我做错了什么,我将不胜感激。
from tkinter import *
import tkinter as tk
window = tk.Tk()
import os
#Must Access this to continue.
def checkPassword():
password = "Starfish"
def enteredPassword():
passwordEntry.get()
if password == enteredPassword:
confirmLabel.config(text="Access granted")
else:
confirmLabel.config(text="Access denied")
passwordLabel = tk.Label(window, text="Password:")
passwordEntry = tk.Entry(window, show="*")
button = tk.Button(window, text="Enter", command=checkPassword)
confirmLabel = tk.Label(window)
passwordLabel.pack()
passwordEntry.pack()
button.pack()
confirmLabel.pack()
window.mainloop()
这是我的代码总结如此:
from tkinter import *
import tkinter as tk
window = tk.Tk()
import os
#Must Access this to continue.
def checkPassword():
password = "Starfish"
def enteredPassword():
passwordEntry.get()
if password == enteredPassword:
confirmLabel.config(text="Access granted")
else:
confirmLabel.config(text="Access denied")
passwordLabel = tk.Label(window, text="Password:")
passwordEntry = tk.Entry(window, show="*")
button = tk.Button(window, text="Enter", command=checkPassword)
confirmLabel = tk.Label(window)
passwordLabel.pack()
passwordEntry.pack()
button.pack()
confirmLabel.pack()
window.mainloop()
#Summer_Task1.py
import random
def get_Name():
global Name
global Class
#Inputs pupil's class
while True:
Class = input ("Please select your class: A)Class 1 B)Class 2 C)Class 3 [C1/C2/C3]? : ")
# check if d1a is equal to one of the strings, specified in the list
if Class in ['C1', 'C2', 'C3']:
# process the input
print("Thank you.")
# if it was equal - break from the while loop
break
def start_quiz():
print("Welcome to the Numeracy quiz that will test your basic arithmatic skills!")
Name = input("Please enter your first name and surname: ")
print("Hi " + Name + "!" + " Please ANSWER the following NUMERACY QUESTIONS and then PRESS ENTER to work out whether they are RIGHT or WRONG. Please ENTER a NUMBER.")
print("You will receive a total score at the end of the quiz.")
def Questions():
global score
global questionnumber
Score = 0
questionnumber=0
while questionnumber<10:
questionnumber=questionnumber+1
Turns = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
for n in Turns:
Number1 = random.randint(1, 10)
Number2 = random.randint(1, 10)
Number3 = random.randint(1, 3)
if Number3 == 1:
Answer = Number1 + Number2
Sign = " + "
elif Number3 == 2:
Answer = Number1 - Number2
Sign = " - "
elif Number3 == 3:
Answer = Number1 * Number2
Sign = " x "
print("\nQuestion", n, "\n", Number1, Sign, Number2)
StudentAnswer = int(input("Please answer the question: "))
print("The correct answer is:", Answer)
if StudentAnswer == Answer:
Score = Score + 1
print("Well Done, that is correct!")
else:
print("Sorry that is incorrect!")
def NumberError():
if StudentAnswer not in Turns '1,2,3,4,5,6,7,8,9,10':
print('Please enter a NUMBER.')
else:
return ("\nQuestion")
print("\nName:", Name)
print("\nClass:", Class)
print("\nScore:", Score)
谢谢!
答案 0 :(得分:1)
您可以测试输入类型,而不是将其转换为int
i = input("Please answer the question: ")
if(not type(i) is int):
print('Please enter a NUMBER.')