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
#2
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.
Reply
#3
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/stdt...ng-methods
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
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/stdt...ng-methods


thanks for that, i'll try a few things out and let you know.
Reply
#5
guys, sorry for the late reply, i figured it out Cool
thanks for your help!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I am getting ValueError from running my code PythonMonkey 0 1,485 Dec-26-2021, 06:14 AM
Last Post: PythonMonkey
  Error in the code ->ValueError: could not convert string to float: ' ' eagleboom 1 3,230 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,416 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