0

I'm trying to get the user input value from the function (enter_student_name) and add it into my dictionary function (add_student). But when I print, I get a Boolean value instead of the string that was entered.


Console Example: Enter student name: john

[{'name': True}]

I want it to return [{'name': john}] instead.

students = []

def enter_student_name():
    while True:
        student_name = str.isalpha(input('Enter student name: '))

        if student_name:
        add_student(student_name)
        print(students)
        # enter_student_id()
    else:
        print('Please enter a name only')
        continue


def add_student(name):
    student = {"name": name }
    students.append(student)
2
  • 3
    The if has absolutely nothing to do with it, its the .isalpha() that's turning your string into a boolean. Commented Apr 19, 2018 at 20:30
  • Ah I see, thanks for clearing that up! Commented Apr 19, 2018 at 20:36

3 Answers 3

2

str.isalpha() returns true or false if the string is alpha characters. See https://www.tutorialspoint.com/python/string_isalpha.htm

Instead get the value from input, THEN check for alpha characters:

students = []


def enter_student_name():
    while True:
        student_name = input('Enter student name: ')

        if str.isalpha(student_name):
            add_student(student_name)
            print(students)
            # enter_student_id()
        else:
            print('Please enter a name only')
            continue


def add_student(name):
    student = {"name": name }
    students.append(student)
Sign up to request clarification or add additional context in comments.

Comments

1

Just use str instead of str.isalpha, so instead of:

str.isalpha(input('Enter student name: '))

use

str(input('Enter student name: '))

this will convert any given value to string and make it work.

then use an if condition with the isalpha to check whether if the string contains all letters before making it call the add_student(student_name) function call.

Comments

1

Move the isalpha() to the if statement:

    student_name = input('Enter student name: ')

    if student_name.isalpha():

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.