0

Functions and conditionals

Good day everyone, I`m new at programming and more so in Python, though i presume to have grasped the basic concept of it all, I`m stuck in a predicament though i feel as though it has a very simple solution. Here`s my code: def str_length(mystring) count = mystring return len (count) mystring = input("write what you will: ") # if mystring > 5: # print (str_length(mystring)), "greater than 5") #elif mystring == 5: print (str_length(mystring)), "equal to 5") #else: print (str_length(mystring)), "less than 5) PROBLEM: I`ve tried all possibilities known to me so far, and I keep getting all kinds of erros, because my goal is to count the characters on a string and the number be compared to the "int" 5, i get that, that`s impossible, and I`ve tried all sorts of conversions and the code still doesn`t work, so how can i make this code work?? Any help would be greatly appreciated. Thank you

16th Aug 2020, 8:24 AM
ecosmos
3 Réponses
+ 3
1. Missing semicolon in `str_length` function definition. 2. Indentation problem for the line that reads <mystring> The lines for `if`, `elif` and `else` blocks are commented, but their respective block body are not. Please mind code indentation as it is very important in Python. Check the below linked code to better understand code indentation 👇 https://code.sololearn.com/cT5BRIbkia21/?ref=app And here is a edited version of your code 👇 def str_length(mystring): # need semicolon at end of this `if` statement return len(mystring) # read input mystring = input("write what you will: ") # measure string length length_of_mystring = str_length(mystring) # evaluate string length if length_of_mystring > 5: print (length_of_mystring, "greater than 5") elif mystring == 5: print (length_of_mystring, "equal to 5") else: print (length_of_mystring, "less than 5")
16th Aug 2020, 9:23 AM
Ipang
+ 2
You've done great job. Nothing to say you've already did it. Can you mention your code to make it easier for us to help you with the errors? do this next time https://www.sololearn.com/post/75089/?ref=app
16th Aug 2020, 9:11 AM
‎يوسف سعد‎
‎يوسف سعد‎ - avatar
+ 1
@Ipang, many thanks for your help and instructions, had been struggling with it for a couple of days, and many thanks to you too @يوسف سعد for your input (though the link you referenced is broken, but i get what you mean, thank you)
17th Aug 2020, 8:21 AM
ecosmos