1

I'm super new to coding and I'm trying to make a simple login program for a school project. My code at the start looks like this.

def start():

 c = input("do you want to login or create an account? Type l or c ")

if c != str("l") or str("c"):
    print("please type l or c")
    start()

The first 'c' at the start is transparent (I'm using PyCharm). When I don't indent it, it doesn't go transparent and looks like it would work but It comes up with an indentation error. Why would it do this and is there a way to fix this. When i hover over the c it says "Local variable 'c' value is not used".

thanks for the help

I've tried looking at other forums and I've seen people saying its something to do with an IDE or something but I don't really know what they're talking about and how to fix my problem.

Liam18923
  • 27
  • 1
  • 1
    You have two `c` variables in this code -- one inside `start` and one outside `start`. Is the `if` statement supposed to be inside `start`? – Paul Hankin Mar 05 '23 at 11:40
  • 3
    Btw, your `if` statement won't do what you probably intend it to: see https://stackoverflow.com/questions/20002503/why-does-a-x-or-y-or-z-always-evaluate-to-true-how-can-i-compare-a-to-al?noredirect=1&lq=1 – slothrop Mar 05 '23 at 11:40
  • IDE means Integrated Development Environment - which in your case is PyCharm. However, the problems here are with your code: PyCharm isn't the source of the problem. – slothrop Mar 05 '23 at 11:43
  • 2
    The correct condition is `c not in ["l", "c"]` – Lorenzo Mogicato Mar 05 '23 at 11:49
  • 1
    There is no need to convert a string (`"l"`) to a string. What do you hope to achieve with that? – Matthias Mar 05 '23 at 11:49
  • 1
    Does this answer your question? [What is variable shadowing?](https://stackoverflow.com/questions/53734399/what-is-variable-shadowing) – Jared Smith Mar 05 '23 at 12:01

2 Answers2

0

I believe the if block is supposed to be inside the start()

# function definition
# if l or c is input this function would do nothing
# else it would ask to login or create again
def start(): 
    c = input("do you want to login or create an account? Type l or c ")
    if c != str("l") and c != str("c"):
        print("please type l or c")
        start()

start() # function call to get things running

OR you can try this way too, here we've added a while loop which would keep repeating till the user inputs either l or c:

c = input("do you want to login or create an account? Type l or c ")
while(c not in ["l", "c"]):
    print("please type l or c")
    c = input("do you want to login or create an account? Type l or c ")
sigmapie8
  • 451
  • 3
  • 9
0

First of all your code should look like this

def start():
    c = input("do you want to login or create an account? Type l or c ")

    if c != "l" and c != "c":
        print("please type l or c")
        start()

We have now defined a function named start with everything below as the body of the function. The indents are very important in Python (you should also use tab to indent).

Now to illustrate what can go wrong:

example 1

def start():
    c = input("do you want to login or create an account? Type l or c ")

if c != "l" and c != "c":
    print("please type l or c")
    start()

Now the if-statement is outside the body of start (because of indentation) and the variable c inside of start is unused.

example 2

def start():
c = input("do you want to login or create an account? Type l or c ")

if c != "l" and c != "c":
    print("please type l or c")
    start()

Now the body of start is empty (no indented lines below). This is an indentation error.

If you wanted to define a function which does nothing (as a placeholder), you would do so as follows:

def do_nothing():
    pass
MennoK
  • 436
  • 3
  • 10