+ 3

I'm getting a syntax error in Python and I'm not sure why?

I recently started learning Python and I'm trying to make a guess the number code. On line 12, it says that 'continue' isn't properly in the loop. Can someone explain to me what's going on and what I need to do to fix this, as from what I can see 'continue' is properly in the loop? Here's the code: from random import * print("Welcome to Guess the Number! The number will be between 1 and 100.") x = randint(1,100) a = int(input("Please enter your first guess (as digit/s):")) if a == x: print("Congratulations! Your guess is correct.") print("Enter 'run' to run the program again or 'quit' to quit the program") z = input(":") if z == "run": continue elif z == "quit": break else: print("Invalid input") break Also, what do you put after 'Invalid input'? At the moment I have 'break', but I'm not sure? And if you do happen to spot any other mistakes, please let me know. I'm very new to the programming world and I've only just started this code, so there's bound to be a lot of mistakes in it. Thanks for your time, and have a great day! :D

26th Aug 2019, 2:06 PM
Alice R
Alice R - avatar
7 Respuestas
+ 7
Of course it's not in a loop - Your program has no loops. If statements are conditional statements which execute only once. To form a loop, you need while loops or for loops. E.g. while True: # your code here
26th Aug 2019, 2:26 PM
Hatsy Rei
Hatsy Rei - avatar
+ 3
I would suggest something like: https://code.sololearn.com/c6bl11l9llpM/?ref=app
26th Aug 2019, 2:36 PM
Hatsy Rei
Hatsy Rei - avatar
+ 1
Break and continue are used only in loops (for and while). In your code, there is no loop, so you can't use this keywords. Second error : if z == "run" : #what you want elif z == "quit" : #what you want z is a string and can only be compared with others strings. To declare a string, that's between quotes or double-quotes (E.g "run"). In your code, you use run and quit as if that are variables, but because run and quit aren't declared, it produces an error. I think you have to put the whole code in a loop : from random import * while 1: x = random.randint(1, 100) a = int(.......... #all the code...
26th Aug 2019, 2:24 PM
Théophile
Théophile - avatar
+ 1
break : use it to break the loop ~Example counter = 0 while 1: counter += 1 if counter == 2: break #go out of the loop continue : go to next iteration ~Example for i in range(0, 2): if i == 0: continue #don't go further, go to next iteration else: print(i) #outputs 1
26th Aug 2019, 2:28 PM
Théophile
Théophile - avatar
+ 1
Thanks so much! This has really helped me. I can't believe I didn't realize there wasn't even a loop.
26th Aug 2019, 2:33 PM
Alice R
Alice R - avatar
0
guys PLeASE HELP ME im stuck on this for decades now im getting a error on the randint line (random.randint (1, sw-1)): import random import curses 5 = curses.initscr() curses .curs_set(0) sh,sw = s.getmaxyx() w = cursess.newwin (sh, sw, 0, 0) w.keypad(1) w.timeout(100) snk_x = sw/4 snk_y = sh/2 snake = [ [snk_y,snk_x], [snk_y, snk_x-1], [snk_y, snk_x-2] ] food = [sh/2, sw/2] w.addch (food[0], food [1], curses. ACS_PI) key = curses.KEY_RIGHT while True: next_key = w.getch() key = key if next_key ==-1 else next_key if snake [0][0] in [0, sh] or snake [0][1] in [0, sw] or snake[1:]: curses.endwin() quit() new_head = [snake [0][0], snake [0][1]] if key == curses.KEY_DOWN: new_head[0] += 1 if key == curses.KEY_UP: new_head[0] -= 1 if key == curses.KEY_LEFT: new_head[1] -= 1 if key == curses.KEY_RIGHT: new_head[1] += 1 snake.insert (0,new_head) if snake [0] == food: food = None while food is None: nf = [ random.randint (1, sh-1) random.randint (1, sw-1) ] food = nf if nf not in snake else None w.addch (food[0], food [1], curses.ACS_PI) else: tail = snake.pop () w.addch(tail[0], tail[1], ' ')
24th Mar 2022, 12:08 AM
HELPMEPLEASE