Anonimo
Anonimo ha chiesto in Computer e InternetProgrammazione e Design · 1 mese fa

Help me!! I wrote Python program that make a number guessed but I still get that the number I input is lower than the secret number.?

Here is the code:

import randomguess = 0guessed = Falsesecret = random.randint(1, 1001)while guessed == False:    print('Write a number\n')    input(guess)    if guess < secret:        print('The secret number is higher')    elif guess > secret:        print('The secret number is lower')    elif guess == secret:        guessed == Trueprint('You are right!' + str(guess) + ' is the secret number!')

Aggiornamento:

import random

guess = 0

guessed = False

secret = random.randint(1, 1001)

while guessed == False:

    print('Write a number\n')

    input(guess)

    if guess < secret:

        print('The secret number is higher')

    elif guess > secret:

        print('The secret number is lower')

    elif guess == secret:

        guessed == True

print('You are right!' + str(guess) + ' is the secret number!')

1 risposta

Classificazione
  • 1 mese fa
    Risposta preferita

    What you got wrong there is that you misunderstood the syntax of the input function. That should be

    <name of variable you want to read> = input( <message to print on console> );

    For instance in your case you should be replacing the first and second line within the while loop with 

    guess = input('Write a number:\t')

    Moreover I saw that in your last line of the while loop there is a guessed == True but I assume you wanted that to be an assignment, therefore the correct syntax is with a single equal sign: guessed = True

    • ...Mostra tutti i contatti
    • I fixed that by using "guess = int(input('Write a number\n'))", I was trying to convert the number after the input, but now it works, thank you for the help

    • Commenter avatarAccedi per rispondere alle risposte
Altre domande? Fai una domanda e ottieni le risposte che cerchi.