Syntax error in naming game

Asked 2 years ago, Updated 2 years ago, 260 views

I'd like to create an English game to name the fruit.

As a rule of the game, the program selects the name of one fruit and displays the number of characters in a hyphen, so the player enters the alphabet of the name of the fruit that matches the number of characters.The name of the fruit that the program chooses is either fig "fig", kiwi "kiwi", mango "mango", or banana "banana".

The player can enter up to 10 characters and print "You are right!" or "You won!" if the answer is correct.

However, if the correct fruit name is not displayed by the 10th inning, the game ends at that point and prints "You lost!"

Also print "The letter does not appear in the name!" if the name of the fruit selected by the program does not contain the characters entered by the player.

However, if the player enters characters that have already been entered and displayed, it will print "No more improve!" because there is nothing more to improve.

Example 1:

 ----
Input selector: > i

-i-i
Input letter: >g
The letter does not appear in the name!

-i-i
Input letter: >a
The letter does not appear in the name!

-i-i
Input letter: >w

- iwi
Input letter: >k

kiwi
You are right!
You won!

However, if there is a zero first, for example:

Example 2:

---
Input selector: > i

-i-
Input letter: >a
The letter does not appear in the name!

-i-
Input selector: > i
No more improve!

-i-
Input letter: >e
The letter does not appear in the name!

-i-
Input letter: > t
The letter does not appear in the name!

-i-
Input selector: >r
The letter does not appear in the name!

-i-
Input letter: >h
The letter does not appear in the name!

-i-
Input letter: >d
The letter does not appear in the name!

-i-
Input letter: >g
-ig
Input selector: >m
The letter does not appear in the name!
You lost!

So I wrote this code.

import random
fruits=["fig", "kiwi", "mango", "banana" ]
choice_fruit=random.choice(fruits)
choice_fruit_2="-"*len(choice_fruit)
temp=list(choice_fruit_2)

x = 10
while x > 0:
    i = 0
    print(choice_fruit_2)
    player_input=(input("Input letter:"))
    if for word not in choice_fruit:
        print("The letter does not appear in the name")
    elif for word in choice_fruit:
            if player_input==word:
                temp[i] = player_input
            i=i+1
        choice_fruit_2=".join(temp)
        print()
    else:        
        for word in choice_fruit and word in temp: 
        print("No more improvement")
else:
    if choice_fruit==temp 
        print("You are right!")
        print("You won!")
    else:
        print("You lost!")

However, I received this error message.

SyntaxError: invalid syntax (<string>, line 12)

How should I improve it?Thank you for your cooperation.

python python3

2022-09-30 21:58

1 Answers

Read the error message carefully.If you are not good at English, send an error message directly to the translation service.

SyntaxError: invalid syntax (<string>, line 12)
google translate results
The error message states that line 12 is an invalid syntax.
Check line 12 carefully.

 if for word not in choice_fruit:

Such syntax does not exist in python.Re-think the program to ensure proper syntax.


2022-09-30 21:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.