Python Forum
Help, newbie here - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Help, newbie here (/thread-24093.html)



Help, newbie here - SufiyanSadiq - Jan-30-2020

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


RE: Help, newbie here - ibreeden - Jan-30-2020

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.


RE: Help, newbie here - SufiyanSadiq - Jan-30-2020

yep works. Thankyou very much


RE: Help, newbie here - dbrdh - Jan-30-2020

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):
???


RE: Help, newbie here - SufiyanSadiq - Jan-30-2020

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


RE: Help, newbie here - buran - Jan-30-2020

(Jan-30-2020, 05:25 PM)dbrdh Wrote: Is there a hierarchy of logical operators?

look at Operator precedence