import random
def pick_random_word():
word_list = ["python", "c", "java", "swift", "html", "css", "go", "ruby"]
random_word = random.choice(word_list)
return random_word
def make_word_classified(word):
classified_list = ["_" for i in word]
return classified_list
def guess():
word = pick_random_word()
classified_word = make_word_classified(word)
print(*classified_word)
total_attempts = 0
while True:
try:
answer = input("Guess a letter (Write only one letter)>: ").lower()
if len(answer) > 1:
raise Exception
except Exception:
print("Only one letter at a time!")
continue
total_attempts += 1
if total_attempts >= 7:
print("Sorry but you lost!")
try_again = input("Wanna play again? (write y or n) >: ")
if try_again == 'y':
guess()
elif try_again == 'n':
print("Goodbye!")
quit()
for i in range(len(word)):
if answer == word[i]:
classified_word[i] = answer
if "".join(classified_word) == word:
print("You won!")
quit()
print(*classified_word, f"\nTotal attempts left: {7 - total_attempts}")
guess()
Sizce program nasıl? Nasıl geliştirilebilir? Hatalarım neler? Teşekkürler şimdiden.
Son tahmin basariliysa da kayip ediliyor. 6’dan fazla degisik harfi olan kelimeleri bulmak imkansiz. 5 harflide tek hata payi var; 4’te 2.
print(*classified_word) hack olmus gibi, string’i elle uretip tek parametre olarak paslamayi deneyebilirsin.
Exception kendi exception siniflarini yaratirken base class olarak kullanacagin bir class; kullaniciya veya programciya hizli bir mesaj gostermeyeceksen (raise Exception("Yeterli sayida dosya yok")) dogrudan kullanmamalisin. Ozellikle yukaridaki gibi control flow (kod akisi kontrolu) icin kullaniyorsan, duruma guzel isimli bir Exception alt sinifi yapman daha okunakli olur.
Kelime bulan, kelimeyi “_ _” yapan ufak fonksiyonlarin yaninda ana loop heyula gibi kalmis. Tahmin alan ve sonucu basip yeniden oynamayi soran kisimlari da kendi fonksiyonlarina alabilirsin.
import random
_WORD_LIST = [
"python", "c", "java", "swift", "html", "css", "go", "ruby",
"javascript"]
_MAX_FAILED_ATTEMPTS = 7
def _pick_random_word():
return random.choice(_WORD_LIST)
def _make_word_classified(word):
return ["_"] * len(word)
def play_the_game():
target_word = _pick_random_word()
classified_word = _make_word_classified(target_word)
print(*classified_word)
attempts_left = _MAX_FAILED_ATTEMPTS
while attempts_left:
answer = input("Guess a letter (Write only one letter)>: ").lower()
if len(answer) != 1:
print("Exactly one letter is expected!")
continue
attempt_correct = False
for char_idx, target_char in enumerate(target_word):
if answer == target_char:
classified_word[char_idx] = answer
attempt_correct = True
if not attempt_correct:
attempts_left -= 1
if "_" not in classified_word:
print("You won!")
play_again = input("Do you want to play again? (y or n)>: ").lower()
if play_again == "y":
play_the_game()
else:
return
print(*classified_word, f"\nTotal attempts left: {attempts_left}")
print("Sorry but you lost!")
try_again = input("Wanna play again? (write y or n) >: ")
if try_again == "y":
play_the_game()
else:
print("Goodbye!")
if __name__ == "__main__":
play_the_game()
Bunlarin diff’ini de koyarsan veya diff’lerini kolayca gorebilecegimiz bir platforma gecirirsen daha rahat ederiz sanki. Ne yalan soyliyim, tek tek copy-paste edip diff’leyesim gelmedi hic.
Herhangi bir source control platformu (GitLab, GitHub)
Manuel de yapilabilir:
12:20:24 1 aib@vivaldi:/tmp% diff -u a b
--- a 2019-11-16 12:20:14.527980410 +0300
+++ b 2019-11-16 12:20:02.992055154 +0300
@@ -1 +1,2 @@
Bu satir vardi
+Bunu yeni ekledim