Python Forum
Variable is not defined error when trying to use my custom function code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variable is not defined error when trying to use my custom function code
#1
Hi so I recently am transitioning from Snap! to python and im trying to recreate something I did in snap but I am having some problems.

# CONTAINS LETTER BLOCK
def contains_letter(string1,letter):
    for i in len(string1):
        if i==letter:
            print(True)
        else:
            print(False)        

contains_letter(hello,h)
When I put hello and h in it just says undefined variable in the problems section. Also im pretty sure some of that code might not work as well as I was trying to find something that would work with what i want it to do.
buran write Nov-23-2023, 08:48 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
Please use bbtags when posting code.

hello is a string and needs to be 'hello' as well as the letter 'h'

Example:
def contains_letter(string, letter):
    for letters in string:
        if letters == letter:
            return True
    return False

print(contains_letter('hello', 'h'))
output
Output:
True
buran likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
oh okay sorry for not using the bb text I was trying to but I couldn't find the shortcut on the text editor thingy. Also thank you I didn't know that the input had to be in quotations for string inputs. I will also be using the return function lol I didnt know that was a thing in here.
Reply
#4
Input does not need to be in quotes, str literals need to be in quotes.

'hello' - makes a str
"hello" - makes a str
hello = "hello" - makes a variable named hello and assignes it to reference a str.

There is no reason to write your function in Python. This code
print("h" in "hello")
Checks if the str "hello" contains the str "h" and prints True if it does. The "in" operator performs the test.
Reply
#5
(Nov-23-2023, 08:40 AM)menator01 Wrote: Example:
def contains_letter(string, letter):
    for letters in string:
        if letters == letter:
            return True
    return False

print(contains_letter('hello', 'h'))

You can simply that to:

def contains_letter(string, letter):
    return bool(letter in string)
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Variable not defined even though it is CoderMerv 3 296 Mar-28-2024, 02:13 PM
Last Post: Larz60+
  Variable for the value element in the index function?? Learner1 8 667 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Printing the variable from defined function jws 7 1,326 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  Function parameter not writing to variable Karp 5 950 Aug-07-2023, 05:58 PM
Last Post: Karp
  Badly defined python code? Ken76 2 628 May-25-2023, 11:05 PM
Last Post: DigiGod
  Getting NameError for a function that is defined JonWayn 2 1,120 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,912 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  How to print the output of a defined function bshoushtarian 4 1,318 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  Retrieve variable from function labgoggles 2 1,056 Jul-01-2022, 07:23 PM
Last Post: labgoggles
  User-defined function to reset variables? Mark17 3 1,659 May-25-2022, 07:22 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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