简体   繁体   中英

how do i fix this? (else function)

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.

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")

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 double quotes 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")

If you want your main function to only run sometimes, you should put it (and your "another function") in actual functions instead of top level code. Then you can call it as needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM