Python Forum

Full Version: Help, newbie here
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hey i am new to python and i am having some issues with my code.
string = ''
length = 0
input_name = input("Enter your name: ")
length = len(input_name) 
moving_point = 0
ascii_value = 3
while moving_point != length:
    asii_value = int(ord(input_name[moving_point]))
    if ascii_value >= 65 and ascii_value<= 90 or ascii_value >=97 and ascii_value <= 122:
        moving_point += 1
    else:
        print("Please insert a valid name")

    
By the way thi code is to make sure that a person cant enter an invalid name like having any symbols or numbers.
This code as it is runs but it prints "Please enter a valid name" for an infinite number of times.
Thnakyou in advance for your help.
Also our sir has told us not to use any in built functions. Including the for loop. We can only use while if etc
The repeating message "Please insert a valid name" is exactly what one might expect. Because you are not incrementing "moving_point" in that situation the while condition "while moving_point != length" will never change.
Solve this by adding a "break" statement in the "else" branch. Or put "moving_point = length" in the else branch.
yep works. Thankyou very much
Hmmm,

I'm new to Python too. I'm a little curious about this line:


if ascii_value >= 65 and ascii_value<= 90 or ascii_value >=97 and ascii_value <= 122:
Does the interpreter really know how to parse this logic? Is there a hierarchy of logical operators?

I would expect something like this...

if (ascii_value >= 65 and ascii_value<= 90) or (ascii_value >=97 and ascii_value <= 122):
???
(Jan-30-2020, 05:25 PM)dbrdh Wrote: [ -> ]Hmmm,

I'm new to Python too. I'm a little curious about this line:


if ascii_value >= 65 and ascii_value<= 90 or ascii_value >=97 and ascii_value <= 122:
Does the interpreter really know how to parse this logic? Is there a hierarchy of logical operators?

I would expect something like this...

if (ascii_value >= 65 and ascii_value<= 90) or (ascii_value >=97 and ascii_value <= 122):
???
Yeah i was just in a bit of a hurry. I did just that in my actual block of code.Looks more neat!!
(Jan-30-2020, 05:25 PM)dbrdh Wrote: [ -> ]Is there a hierarchy of logical operators?

look at Operator precedence