Python Forum
I need help with ValueError in my code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I need help with ValueError in my code
#1
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).
Reply


Messages In This Thread
I need help with ValueError in my code - by flatman123 - Dec-20-2017, 05:40 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  I am getting ValueError from running my code PythonMonkey 0 1,534 Dec-26-2021, 06:14 AM
Last Post: PythonMonkey
  Error in the code ->ValueError: could not convert string to float: ' ' eagleboom 1 3,272 Nov-29-2019, 06:19 AM
Last Post: ThomasL
  Python- Help with try: input() except ValueError: Loop code to start of sequence Aldi 2 6,495 Mar-08-2018, 03:46 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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