Python checking numbers in a given name

Click For Summary
SUMMARY

The discussion centers on creating a Python program that validates user input for names, ensuring no digits are included. The user encountered an issue where valid names were printed multiple times due to the placement of the print statement within a loop. The solution involves restructuring the code to separate the validation logic from the output, ensuring the name is printed only once after successful validation.

PREREQUISITES
  • Understanding of Python input functions
  • Familiarity with loops and conditional statements in Python
  • Knowledge of string methods such as isdigit()
  • Basic debugging skills in Python
NEXT STEPS
  • Learn about Python exception handling to manage invalid inputs more effectively
  • Explore the use of regular expressions for more complex input validation
  • Study Python functions to modularize the name validation logic
  • Investigate Python's built-in string methods for additional validation techniques
USEFUL FOR

Beginner Python programmers, educators teaching input validation, and developers looking to enhance user input handling in applications.

acurate
Messages
17
Reaction score
1

Homework Statement


I need to write a Python program, which would:

1) Ask for a name (input function)
2) Keep asking for the name if it is not entered correctly(if it has a digit in it)
3) If the name is entered correctly it will print the name

Homework Equations


How should I continue the program? I tried another way, when I enter for example "J4ck" it asks to enter the name again but when I enter "Jack" for the program to print it, it repeats the name "Jack" 4 times in a row. Any suggestions?

The Attempt at a Solution


Code:
while True:
    name = input("Enter a name: ")
    for x in name:
        if x.isdigit():
            break
 
Technology news on Phys.org
Is your instruction to print the name contained within the 'for x in name' loop? If so, the interpreter will print the name as many times as there are letters in the name.
 

Similar threads

  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K