Bu benim ilk gönderimimdir, ancak Google aracılığıyla burada çok fazla harika bilgi buldum. Şu anda, Absolute Beginner için Python Programlama ile çalışıyorum. Kitabın nispeten başlarında meydan okuma sorunlarından birine sıkışmış durumdayım. Buradaki zorluk, rastgele 1 ve 100 arasında seçtiğiniz bir sayıyı tahmin edecek bir program oluşturmaktır.Python'da yeni, buna takılıyorum
Bu noktada, geçerli olmayan yanıtları dışarıda işler ve kurallara göre oynarsanız tahminler yapar. Eğer oyunu dürüst oynarsan, işe yarıyor. Ancak, "daha yüksek" yerine "daha düşük" koyarsanız, programın çöktüğü bir noktaya gelir. Bu işe yarıyor, ancak sayacı sıfırlamayı ve kullanıcıyı bir mesajla kutlamayı tercih ederim.
Neyse, işte kodum şu ana kadar. While döngüsünün ilk “if” cümlesiyle çalıştığım yerde ne yapmaya çalıştığımı görebilirsiniz. Ne düşünüyorsun?
#Computer Guesses Number Program
import random
print("Please select a number between 1 and 100.")
print("As I attempt to guess, you may respond with Higher, Lower, or Yes.")
input("\nPress enter to continue.")
#Start Guessing in the Middle
guess = 50
response = input("\nIs your number " + str(guess) + "?")
#Narrow down guessing further
if response.lower() == "higher":
guess = 75
lowerbound = 50
upperbound = 100
response = input("\nIs your number " + str(guess) + "?")
else:
guess = 25
lowerbound = 1
upperbound = 50
response = input("\mIs your number " + str(guess) + "?")
#Guess loop - eliminates invalid responses and need for capitalization
while response.lower() != "yes":
if lowerbound > upperbound or upperbound < lowerbound:
print("You've been dishonest, let's try again.")
guess = 50
lowerbound = 1
upperbound = 100
elif response.lower() == "higher":
lowerbound = guess + 1
guess = random.randint(lowerbound, upperbound)
elif response.lower() == "lower":
upperbound = guess - 1
guess = random.randint(lowerbound, upperbound)
else:
print("The response options are 'Higher', 'Lower', or 'Yes'.")
response = input("Is this your number " + str(guess) + "?")
print("\nI knew it was ", guess, "the whole time!")
input("\n\nPress enter to exit.")
Yardımlarınız için teşekkürler, bu çok mantıklı. Orada yakın olduğumu hissettim ama göremedim. – dstana
Bir şey değil. Sorunuzu çözüldüğü şekilde işaretlemek için lütfen [yanıtı kabul et] 'i (http://meta.stackexchange.com/a/5235/141542) unutmayın. – poke