Coding Global Background
Coding Global

Beginner Help - Why isn't my code working?

Archiviert 2 years ago
19 Nachrichten
1 Mitglieder
Erstellt 2 years ago
Aktualisiert 2 years ago
In Discord öffnen
S
♯ louise !
Verified
I've only just gotten it working, but now it's not giving the output I want. I wanted it to continue replying with 'PC response: (whatever it says)' but it only says it once and then never again, how do I make sure it keeps going?

Here's my code:
(sorry for the bad code i just started last month :what: )
 
# GENERATING THE NUMBERS (FOUR DIGITS - SIMPLE - LIMITED THREE AMOUNT OF GUESSES)
import random
code = list(range(10))
random.shuffle(code)
secret_code = ''.join(map(str, code[:4]))

print(secret_code)

# ASKING THE USER
guess = input("Enter your guess: ")

# GUESSING GAME
while guess != code:
    if secret_code[0] == guess[0]:
      print('PC response: *')
    elif secret_code[0] == guess[1]:
      print('PC response: +')
    elif secret_code[1] == guess[1]:
      print('PC response: *')
    elif secret_code[1] == guess[0]:
      print('PC response: +')
    elif secret_code[2] == guess[2]:
      print('PC response: *')
    elif secret_code[2] == guess[3]:
      print('PC response: +')
    else:
      print('PC response:  ')
    # RAN OUT OF GUESSES
    number_of_guesses = 0
    while True:
      number_of_guesses += 1
      # you put a 'one' to increment the number of guesses after every guess
    if number_of_guesses == 3:
      # dont put the print statement out of the loop or else it will never be reaached 
      print('PC response: You have run out of guesses! Game over!')
    
    
# ASK HERE SO IT DOESN'T LOOP    
print("Congratulations! You guessed the number!")

Antworten (19)