Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help, newbie here
#1
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
Reply
#2
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.
Reply
#3
yep works. Thankyou very much
Reply
#4
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):
???
Reply
#5
(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!!
Reply
#6
(Jan-30-2020, 05:25 PM)dbrdh Wrote: Is there a hierarchy of logical operators?

look at Operator precedence
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020