Syntax Error on while command in python 3 program - python-3.x

I am a beginner in python 3. Made a program. it was working fine but then i change it a bit and now its giving syntax error on line 2 of give code.I have only uploaded a patch of total code
i tired retyping it. I also tried executing it on different IDEs. But nothing worked
print('You have a total of {} guesses'.format(len(answer))
while count < len(answer):
guess = input('Enter the letter: ') #input of the guesses
guess = guess.lower() # all words are in lowercase
count += 1
print('You have {} guesses left'.format(len(answer)-count) #to tell the total guess left
# we need to iterate over the answer to check guess in answer
for i in range(len(answer)):
if answer[i] == guess:
display[i] = guess #replacing corresponding '_' with letter
print(' '.join(display))
if display == answer: #to check and stop unneccessary repitations
break
if display == answer:
print('You Won!!!!')
else:
print('You Lost!!!!')
again = input('Do you want to play again(Y/N)?' )
if again.lower() == 'n':
is_again = False
print('Good Bye!!!')
it is giving a syntax error on while command. it is like this:
while count < len(answer):
^
SyntaxError: invalid syntax

You're missing a parenthesis! The line:
print('You have {} guesses left'.format(len(answer)-count)
^----------------------------------------------------?
^-----------------^
^------^
is missing a matching parenthesis for the print. It should be:
print('You have {} guesses left'.format(len(answer)-count))
Your first line is missing a parenthesis as well:
print('You have a total of {} guesses'.format(len(answer))
Should be:
print('You have a total of {} guesses'.format(len(answer)))

Related

Python program won't append more than one value

Whenever I try to append(guesses) to the all_guesses variable it seemingly replaces the existing value from the previous loop. I want the program to record down all the player's number of guesses per game round but it only record the most recent value. I made sure the variable isn't in the while loop so that it doesn't overwrite it, so what's wrong? I'm really new to python programming so I can't seem to figure this out. Each time I run the loop the guessed and all_guesses values are reset to their original.
This is a snippet of my program:
def main():
guesses = 0
guessed = []
all_guesses = []
guess = input('\nPlease guess a letter: ').lower()
letter = 'abcdefghi'
answer = random.choice(letter)
while len(guess) != 1 or guess not in letter:
print("\nInvalid entry! One alphabet only.")
guess = input('Please guess a letter: ')
while len(guess) < 2 and guess in letter:
if guess in guessed:
guess = input("\nYou've already guessed that! Try again: ").lower()
else:
if guess == answer:
guesses = guesses + 1
played = played + 1
print("\nCongratulations, that is correct!")
replay = input('Would you like to play again? Type y/n: ').lower()
all_guesses.append(guesses)
The short answer would be that all_guesses needs to be a global defined outside of main and the replay logic also needs to wrapped around main.
You seem to be missing logic, as you never modify guessed but expect to find things in there. And there are dead ends and other missing parts to the code. As best as I can guess, this is roughly what you're trying to do:
from random import choice
from string import ascii_lowercase as LETTERS
all_guesses = []
def main():
guessed = []
answer = choice(LETTERS)
guess = input('\nPlease guess a letter: ').lower()
while len(guess) != 1 or guess not in LETTERS:
print("\nInvalid entry! One alphabet only.")
guess = input('Please guess a letter: ').lower()
while len(guess) == 1 and guess in LETTERS:
if guess in guessed:
guess = input("\nYou've already guessed that! Try again: ").lower()
continue
guessed.append(guess)
if guess == answer:
print("\nCongratulations, that is correct!")
break
guess = input("\nIt's not that letter. Try again: ").lower()
all_guesses.append(len(guessed))
while True:
main()
replay = input('Would you like to play again? Type y/n: ').lower()
if replay == 'n':
break
print(all_guesses)

what is this syntax error? I don't even know what the error is. I m just beginning

number = input("Please enter your number:")
number = int(number)
if number % 10 == 0 :
print("Yes the number is a multiple of 10.")
elif :
print("No the number is not a multiple of 10.")
else :
print("Invalid number!")
this code gives the following output:
File "multiple_10.py", line 5
elif :
^
SyntaxError: invalid syntax
You need to provide a condition when you use elif--otherwise just use else. Check out the python docs here: https://docs.python.org/3/tutorial/controlflow.html?highlight=elif.
elif need a condition, add a condition or use else
The main problem is elif need a condition, but in your code that is missing
I don't know what condition is suitable for your code but I tried this and its working fine
number = input("Please enter your number:")
number = int(number)
number = number % 10
print(number)
if number == 0 :
print("Yes the number is a multiple of 10.")
elif number > 0 :
print("No the number is not a multiple of 10.")
else :
print("Invalid number!")

How to fix unexpected EOF while parsing in python 3.6?

Im getting the EOF at the end of the program when i try to run it. i dont really know how to fix it. at first i was getting "if" as an invalid syntax but i think i was able to fix that. thanks for the help
while True:
try:
print("Do you want to enter a number?")
print("y - yes")
print("n - no")
choice = int(input("Enter here: "))
if choice == y:
print("")
count = number
for indice in range(1,number + 1, 1):
print(number + indice)
print("")
print("All done")
You're missing a except to match try.
Note that there are other issues with your code that will break it, even once you've added except. For example,
if choice == y:
...
This should be 'y' instead of y. As it is, y is expected to be a variable, but you're looking to match on the user input 'y' or 'n'.
Also, if you want a string input, then:
choice = int(input("Enter here: "))
will throw an error if you enter, say, 'y':
invalid literal for int() with base 10: 'y'
Try taking things one line at a time and making sure you understand what's supposed to happen at each point, and test it. Then put them together.

How to trace the input error in my hangman program?

I have an error in the input section of the code. Normally it is working well but in this code, I do not know why this is showing error.
print("Welcome to the game, Hangman!")
print("I am thinking of a word that is ",len(secretWord)," letters long.")
print("-------------")
n=8
G_new=''
k=''
while n>0 and secretWord!=G_new:
print("You have ",n," guesses left.")
G_old=[G_new,]
print("Available letters: ",getAvailableLetters (G_old)
**k=input("Please guess a letter: ")** #error in this line
g=k.lower()
G_new=G_old+[g,]
if isWordGuessed(secretWord, G_new)==True and g not in G_old:
print("Good guess: ",getGuessedWord(secretWord, G_new))
elif isWordGuessed(secretWord, G_new)==True and g in G_old:
print("Oops! You've already guessed that letter: ",getGuessedWord(secretWord, G_old))
else:
print("Oops! That letter is not in my word: ",getGuessedWord(secretWord, G_old))
n-=1
print("-------------")
if n==0 and secretWord!=G_new:
print("Sorry, you ran out of guesses. The word was ",str(secretWord),".")
elif n>=0 and secretWord==G_new:
print("Congratulations, you won!")
Count the ( and ):
print("Available letters: ",getAvailableLetters (G_old)
1 2 2 1????
As a general rule, if you get a syntax error that says "unexpected" or whatever, the error is invariably NEVER on the line in the error message - it's actually somewhere EARLIER, where you made a typo. The specified error location is merely the first place where the parser realized there IS a problem.

cannot fix this "else" statement error

the second "else" statement gives a syntax error. I don't understand why. what is wrong with the code?
Pardon me, still a beginner
while True:
guess = input("Guess a letter or the whole word: ")
if guess == word:
print("Yaye, you've won and have saved my neck!")
break
else:
for letter in letters:
if letter in letters:
continue
else:
guesses -= 1
word_guess(guesses)
if guesses == 0:
break
You can see in the Python 3 flow control documentation an example of an if statement. It can only have one else statement because that is what is run when all other cases (if and elif) didn't match. When are you expecting the second else to run?
As was pointed out in another answer, indentation in python matters.
Is this perhaps the indentation you are looking for?
while True:
guess = input("Guess a letter or the whole word: ")
if guess == word:
print("Yaye, you've won and have saved my neck!")
break
else:
for letter in letters:
if letter in letters:
continue
else:
guesses -= 1
word_guess(guesses)
if guesses == 0:
break

Resources