python代码函数不适用于算术测验,密码不适用

2024-10-06 12:35:08 发布

您现在位置:Python中文网/ 问答频道 /正文

请给我一些代码方面的帮助,因为函数对NumberError()不起作用。关键是,如果在回答数学测验时你错误地键入了一个字母,它会说“请键入一个数字”,但是我无法让它工作,因为不断出现错误:

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)

谢谢!你知道吗


Tags: thetextinimportifaccessdefwindow