Python Forum
I need help with ValueError in my code - 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: I need help with ValueError in my code (/thread-7082.html)



I need help with ValueError in my code - flatman123 - Dec-20-2017

Hello everyone, i'm a newbie @ python. about 3-4 months in..-- also new to the forum.

I've been having some problems with some code. The instructions requested for me to invert a triangle.
I was able execute this code.. but it breaks when an empty strings is inputted.

Here is my code: ( When you call the function with with a numeric-string and an integer, the code runs without any issues).

def inverted_tri(t):
another_astk = 0
space = 30
if type(t) == str:
t = int(t) # if end-users enters a string, this converts it to an integer
for x in reversed(range(t)):
if x % 2 == 1:
print(' ' * space,'#' * x)
space = space + 1

inverted_tri(' ') # Calling this function with an empty string breaks it.



here is error message:


ValueError: invalid literal for int() with base 10: ' '


Now I'm trying to figure out a way to prevent an end-user from entering an empty string..( preventing them from breaking the program)...but i'm lost..

I would greatly appreciate some help.. Please do not type out any code containing your examples as to how you would do it; as it would not help me progress Cool ... I would much rather you point me in the right direction ( i.e maybe an article on a particular function and or statement that would help me clear the issue).


RE: I need help with ValueError in my code - squenson - Dec-20-2017

Consider this logic (this is not python language, as you requested):
Output:
Initialize t to '' While t is '' ask the user to input a number
As long as t is equal to '' the user will be asked to enter a value.


RE: I need help with ValueError in my code - wavic - Dec-20-2017

Hello!
First, put your code between Python code tags ( BBcode )! To preserve indentation use CTRL+SHIFT+V key combo to paste the code.

An empty string cannot be converted into an integer. The string is 't', right? You can check for an empty string by if statement easy: if t:. This will return False if 't' == "". Also, you can check if the strings contain digits only by t.isdigit().

Ref: https://docs.python.org/3.5/library/stdtypes.html#string-methods


RE: I need help with ValueError in my code - flatman123 - Dec-20-2017

Sorry about that:

def inverted_tri(t):
     another_astk = 0
     space = 30
     if type(t) == str:
     t = int(t) # if end-users enters a string, this converts it to an integer
     
     for x in reversed(range(t)):
        if x % 2 == 1:
        print(' ' * space,'#' * x)
        space = space + 1

inverted_tri(' ')

(Dec-20-2017, 08:44 PM)wavic Wrote: Hello!
First, put your code between Python code tags ( BBcode )! To preserve indentation use CTRL+SHIFT+V key combo to paste the code.

An empty string cannot be converted into an integer. The string is 't', right? You can check for an empty string by if statement easy: if t:. This will return False if 't' == "". Also, you can check if the strings contain digits only by t.isdigit().

Ref: https://docs.python.org/3.5/library/stdtypes.html#string-methods


thanks for that, i'll try a few things out and let you know.


RE: I need help with ValueError in my code - flatman123 - Dec-30-2017

guys, sorry for the late reply, i figured it out Cool
thanks for your help!