Home > OS >  how do i fix this? (else function)
how do i fix this? (else function)

Time:09-20

I was trying functions since im new to python.

then i ran into a error.

lol = input(“Hello!\n”)
if lol == “Hi!”:
  print(“Hello Again!”)
else:
  print(“Error: Syntax Unavailable.”)

it works normally but if another function is runned, it will run automatically

Like:

# — Another Function
s = input(“RUN FUNCTION?”)
if s == “Yes”:
  x = 1
  while 1 < 10:
    print(x)
    x  = 1
# - Main Function
z = input(“Hello Lol\n”)
if z == “Hi!”:
  print(“Hello!”)
else:
  print(“Error”)

OUTPUT:

RUN FUNCTION?Yes
1
2
3
4
5
6
7
8
9
10
Error

I want the “Error” Message to respond ONLY when the Syntax is unindentified.

CodePudding user response:

For the second code to give the output given by you, you need to use while x<=10 for it to stop executing once x is not less than or equal to 10 instead of while 1<10 as this case will always be true as 1 is always less than 10 the while loop will keep on running. Also use inverted commas for the characters.

You can try this code for it to work:

# — Another Function
s = input("RUN FUNCTION?")
if s == "Yes":
  x = 1
  while x <=10:
    print(x)
    x  = 1
    
# - Main Function
z = input("Hello Lol\n")
if z == "Hi!":
  print("Hello!")
else:
  print("Error")

CodePudding user response:

You shouldn't be using inverted commas and what I understand from your code you wanted to achieve, can be achieved like this:

s = input("RUN FUNCTION?")
if s == "Yes":
  x = 1
  while x < 10:
    print(x)
    x  = 1
# - Main Function
z = input("Hello Lol\n")
if z == "Hi!":
  print("Hello!")
else:
  print("Error")
  • Related