2017-03-01 2 views
0

안녕하세요 저는 프로그래밍에 완전히 익숙하지 않아서 파이썬을 가르치려고 노력했습니다. 나는 단어를 선택한 다음 편지를 뒤섞어서 사용자가 추측을 입력하도록하는 프로그램을 만들려고 노력했습니다. 3 번 시도합니다. 제가하는 데 문제는 잘못된 대답이 선택한 단어의 편지를 reshuffles 입력 또는 완전히 다른 단어를 반환 할 때 여기 내 코드입니다 :임의의 단어 게임 python 3.5

import random 
import sys 

##Welcome message 
print ("""\tWelcome to the scrambler, 
    select [E]asy, [M]edium or [H]ard 
    and you have to guess the word""") 

##Select difficulty 
difficulty = input("> ") 
difficulty = difficulty.upper() 

##For counting number of guesses it takes 
tries = 0 

while tries < 3: 
    tries += 1 

##Starting the game on easy 
if difficulty == 'E': 
    words = ['teeth', 'heart', 'police', 'select', 'monkey'] 
    chosen = random.choice(words) 
    letters = list(chosen) 
    random.shuffle(letters) 
    scrambled = ''.join(letters) 
    print (scrambled) 

    guess = input("> ") 

    if guess == chosen: 
     print ("Congratulations!") 
     break 
    else: 
     print ("you suck") 

else: 
    print("no good") 
    sys.exit(0) 

당신이 볼 수 있듯이 나는 단지로 들어 왔 아주 쉽게, 나는 조각으로 그것을하려고 노력하고 있었고, 다른 문제들을 극복 할 수 있었다. 그러나 나는 가지고있는 것을 고치는 것처럼 보이지 않는다. 어떤 문제라도 내가 가지고있는 문제 또는 내 코드에서 발견 할 수있는 다른 문제로 인해 도움을받을 수 있습니다.

+0

다음 단어/스크램블 _before_ 재시도 루프를 선택하십시오 ... –

+0

while 루프에 의해 선택되도록 if 블록을 들여 쓸 수 있습니다. – Ohjeah

+0

@Ohjeah 생각했지만 버그 설명에 들여 쓰기가 명확하게 나타났습니다. 게시 할 때 오류가 발생했습니다. –

답변

1

게임에 대한 몇 가지 개선점과 수정 사항이 있습니다.

import random 
import sys 

# Game configuration 
max_tries = 3 

# Global vars 
tries_left = max_tries 

# Welcome message 
print("""\tWelcome to the scrambler, 
select [E]asy, [M]edium or [H]ard 
and you have to guess the word""") 


# Select difficulty 
difficulty = input("> ") 
difficulty = difficulty.upper() 

if difficulty == 'E': 
    words = ['teeth', 'heart', 'police', 'select', 'monkey'] 
    chosen = random.choice(words) 
    letters = list(chosen) 
    random.shuffle(letters) 
    scrambled = ''.join(letters) 
else: 
    print("no good") 
    sys.exit(0) 

# Now the scrambled word fixed until the end of the game 

# Game loop 
print("Try to guess the word: ", scrambled, " (", tries_left, " tries left)") 

while tries_left > 0: 
    print(scrambled) 
    guess = input("> ") 

    if guess == chosen: 
     print("Congratulations!") 
     break 
    else: 
     print("You suck, try again?") 
     tries_left -= 1 

당신이 뭔가를 이해하지 못한다고 말하면, 당신을 도울 수있는 즐거움이 될 것입니다.

+0

감사합니다. 거대한 도움! 바라건대 지금 나는 매체와 어려운 말로 쪼개 질 수있다. 이것은 내가 스스로 해보려 고 노력한 가장 힘든 일이다. 나는 완전한 멍청한 놈처럼 보이지 않기 위해 여기에 게시하고 싶지는 않다. 그러나 지금은 나는 다시 한 번 기뻤다. –