Ruby_Red22 Ruby_Red22 - 3 months ago 12
Python Question

python code- function does not work for arithmatic quiz and password does not work

Please could I have some help with my code, as the function does not work for the

NumberError()
The point is that if when answering the maths quiz you type in a letter by mistake, it will say 'PLEASE TYPE IN A NUMBER', however I cannot get it to work as an error keeps coming up:

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'


Here is what I have attempted so far:

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")


Also the password option to access the quiz does not fully work: Here is what I have so far and I would appreciate it if I could have some help on what I need to do right and what I am doing wrong.

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()


THIS IS MY OVERALL PIECE OF CODE SO FAR:

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)


Thanks!

Answer

You could test the input type rather than casting it as an int

i = input("Please answer the question: ")
if(not type(i) is int):
    print('Please enter a NUMBER.')
Comments