IcarusFlight - 1 year ago 159
Python Question

# Accepting only letters as input

My code is supposed to only accept letters (for example Jack would be accepted, and jack1 would not be accepted).

It prompts the user for their first and last name then stores them. Once I had written the code for the first name, I tested it to see if I had written the code correctly for the first name, but it kept giving me this error .

In the answers can you please show how to make this work with only numbers being allowed?

Code

``````import random

operators = ["+", "-", "*"]

def greeting(first_name, last_name):
print ("Hello", first_name + " " + last_name)
Play = input('Are you ready to begin?')
if Play == 'yes':
print("Great", first_name + ", lets begin")
else:
greeting(first_name, last_name)

def Players_input():
print ("Welcome to the Arithmetic Quiz")
if all(x.isalpha() or x.isspace() for x in first_name):
greeting(first_name, last_name)
else:
print("Only alphabetical letters and spaces: no")

Players_input()

score = 0
for i in range(10):
first_number = random.randint(1,12)
second_number = random.randint(1,12)
op = random.choice(operators)

print (first_number, op, second_number, " = ?")

if op == "+":
elif op == "-":
elif op == "*":

print("Well Done!")
score += 1
else:
print ("Sorry but thats the wrong answer, the right answer is: " + str(right_answer) + ". Better luck next time")

print (first_name, "Your final score in the Arithmetic Quiz is", str(score), "out of 10")
``````

The answer for the first problem:

You never defined first_name outside of `Players_input`. This value is just stored inside the function, and get's deleted afterwards. (more about this in the link added by gjttt1) There are two ways to solve this:

• You could make first_name global. But this is a bad style, so I wouldn't use this option. You would add `global first_name` at some point in `Players_input`, before it is written to (so either before or directly after the first print call)
• You could return first_name, this is the preferred way. Add a `return first_name` at the end of `Players_input`, and replace `Players_input()` with `first_name = Players_input()`.

The answer to the second problem:

Just use this function instead of `int(input())` (replace this line with `int_input()`):

``````def int_input(prompt="", error_message="You didn't enter an integer!"):
while True:  # repeat this until the function returns
inp = input(prompt)  # get the input after autputting the prompt.
try:  # Try to...
return int(inp)  # turn it into an integer. If it works, return it.
except ValueError:  # If it didn't work, it raised a ValueError. In this case...
if error_message:  # print the error_message if it is not "", false or None.
print(error_message)
``````

Then you have a third problem: You should just use lowercase letters in function names, to distinguish them from classes. This is just about your style, but it'll certainly help to develop a good, clear coding style.

I hope I could help,

CodenameLambda