Sto iniziando a imparare Python e sto cercando di ottimizzare questo gioco di ricerca in bisezione.
high = 100 low = 0 guess = (high + low)/2 print("Please think of a number between 0 and 100!") guessing = True while guessing: print("Is your secret number " + str(guess) + "?") pointer = raw_input("Enter "h" to indicate the guess is too high. Enter "l" to indicate the guess is too low. Enter "c" to indicate I guessed correctly.") if pointer == "h": high = guess guess = (low + guess)/2 elif pointer == "l": low = guess guess = (high + guess)/2 elif pointer == "c": guessing = False else: print("Sorry, I did not understand your input.") print("Game over. Your secret number was: " + str(guess))
Risposta
Alcune cose penso che migliorerebbero il tuo codice, il che è abbastanza corretto:
- Avendo variabili per
highelow, non dovresti “t codificare i loro valori nellaperturaprint. - Dovresti usare
//per assicurarti di ottenere una divisione intera. - Puoi scrivere
guess = (low + high) // 2solo una volta, se lo inserisci come prima riga allinterno delwhileloop. - Quando controlli
pointer, potresti convertirlo prima in lettere minuscole, per assicurarti che siano compresi siahcheH. - Rendi il tuo codice conforme a PEP8 su cose come il massimo lin e length.
- Utilizzando il
formatmetodo distrpuoi rendere più chiaro ciò che stai stampando.
Mettere tutto insieme:
high, low = 100, 0 print("Please think of a number between {0} and {1}!".format(low, high)) guessing = True while guessing: guess = (low + high) // 2 print("Is your secret number {0}?".format(guess)) pointer = raw_input("Enter "h" to indicate the guess is too high. " "Enter "l" to indicate the guess is too low. " "Enter "c" to indicate I guessed correctly.").lower() if pointer == "h" : high = guess elif pointer == "l" : low = guess elif pointer == "c": guessing = False else: print("Sorry, I did not understand your input.") print("Game over. Your secret number was {0}.".format(guess))
Risposta
Oltre ai punti di Jaime.
-
Sbarazzati del flag
guessinge fai un ciclo infinito con una pausa dichiarazione. -
pointerè un nome davvero strano per quella variabile, soprattutto perché significa qualcosaltro in altri linguaggi di programmazione.
Risposta
Completando la risposta data da Jamie, con unosservazione:
Quando tu digita "c", anche se il numero non è quello a cui pensi, stampa sempre questa parte di codice print("Game over. Your secret number was {0}."
Quindi, per evitare ciò, devi testare anche (str(numbers) == str(guess)) nel ramo di (response == "c"):
high, low = 100, 0 guess = (low + high) // 2 numbers = raw_input("Please think of a number between {0} and {1}!".format(low, high)) guessing = True while guessing: print("Is your secret number {0}?".format(guess)) response = raw_input("Enter "h" to indicate the guess is too high. " "Enter "l" to indicate the guess is too low. " "Enter "c" to indicate I guessed correctly.").lower() if response == "h" : high = guess elif response == "l" : low = guess elif (response == "c") and (str(numbers) == str(guess)) : print("Game over. Your secret number was {0}.".format(guess)) break else: print("Sorry, I did not understand your input.") guess = (low + high) // 2
Commenti
- Il punto di questo gioco dipotesi è che lutente non inserisce mai il numero effettivo poiché è un segreto .
Risposta
Oltre alle altre risposte: quando scelgo il numero 100 come segreto , il codice verrà eseguito in un ciclo infinito, chiedendomi ripetutamente come si confronta con 99. Puoi limitare ulteriormente lintervallo dopo aver ottenuto una risposta da parte dellutente.