Problem with while loop

python

#1

Hello to everyone !
I have this requirments to make a code and i don’t understand where I am wrong.
In the body of the str_analysis() function:
Check if string is digits
if digits: convert to int and check if greater than 99
if greater than 99 print a message about a "big number"
if not greater than 99 print message about "small number"
check if string isalpha then (since not digits)
if isalpha print message about being all alpha
if not isalpha print a message about being neither all alpha nor all digit
call the function with a string from user input
Run and test your code before submitting

str_analysis (answear):
    x = ""
    while x != "":
        if x.isdigit():
            return True
            if int(x) > 99:
                print ("WoW this is a big number")
                if x.isalpha():
                    print ("This is a word with alphabetical characters")                
            else:
                print("Try again is a small number")
                else:
                    print("not an alpha or digit character")
user_input=input("Enter a word or a number\n")
print(str_analysis(user_input))

#2

why would you use a while loop? I don’t see a reason to add a while loop, just like there is no reason to have a variable named x

the function parameter (answear) contains the user input, so you can use that

then you can check if answear is a digit, elif check if answear is alpha, else print invalid

the flow of your program isn’t logic at the moment


#3

I used the while loop because is another requirment. The program will call str_analysis() with a string argument from input collected within a while loop. The while loop will test if input is empty (an empty string “”) and continue to loop and gather input until the user submits at least 1 character (input cannot be empty).


#4

but then you didn’t implement the while loop correctly?

then it would be something like:

user_input=input("Enter a word or a number\n")
while user_input == "":
    user_input=input("input can't be empty, enter a word or a number\n")

or does this need to be part of the function?


#5


Yes this need to be part of function.


#6


Almost is done your explanations helped me a lot can you tell me what is wrong in the code sintax ?


#7

the design of your program is certainly much better, nicely done :slight_smile: You understand why this changes where so important?

I am not sure what is wrong with .isalpha() can you copy paste your code to the forum so i can run it?


#8

Yes, of course

def str_analysis (answear):
        if answear.isdigit():
            if int(answear) > 99:
                print (user_input,"is a pretty big number")
            else:
                print(user_input,"is a smaller number than expected")
        if answear.isaplpha():
                print ("This is a word with alphabetical characters")
        else:
            print("not an alpha or digit character")
user_input=input("Enter a word or a number\n")
while user_input == "":
    user_input=input("input can't be empty, enter a word or a number\n")
print(str_analysis(user_input))

#9

i would make it elif answear.isaplpha():, given there is no point in checking if its alpha after checking its digit, but not sure yet why you get the error

duh, that took me to long. you misspelled isalpha


#10

I made a lot of mistakes
This program requires the use of
while loop to get non-empty input
if, else
if, else (nested)
.isdigit() check for integer only input
.isalpha() check for alphabetic only input


#11

yes, nice challenge. Did you fix it and understand all of it now? :slight_smile:


#12

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.