これが何をするかの簡単な概要です。ユーザーが推測する数字と 2 つの記号 (+ と *) の秘密の文字列を生成します。ハングマンかな。私が抱えていた他のすべての問題を解決し、1.5まで減っています。私は初心者です。
まず、 1 つの問題は、(IDLE で F5 を使用して) 実行しても何も表示されないことがあるということです。「>>」だけで、シェルを閉じて再実行する必要があります。
第二に、再びプレーするという申し出を勝ち取って辞退した後でも、推測を入力するよう求められます。while ループがまだ実行されていると思いますが、どうすれば停止できますか?
他にも見落としている問題があるかもしれませんが、少し調べてみたところ、すべてうまくいっているようです。より良いものにするための提案があれば (高度すぎるものはありません)、遠慮なく指摘してください!
だからここに私が持っているものがあります:
from random import randint, choice
maxguesses = 11
wins = 0
losses = 0
points = 0
games = 0
numguesses = 0
allowed = ('0','1','2','3','4','5','6','7','8','9','0','+','*')
guesses = ''
def genform():
global secret
global guesses
global maxguesses
sym = choice(['++','**','+*','*+'])
num = ''
max = randint(3,9)
for i in range(max):
num = num + str(randint(0,9))
secret = sym + num
guesses = ''
maxguesses = len(secret)+2
return secret
def evaluate(b):
global evaluated
evaluated = ''
s1 = secret[0]
s2 = secret[1]
for i in range(2,len(secret)-1):
evaluated = evaluated + secret[i]
if (i%2 == 0):
evaluated = evaluated + s1
else:
evaluated = evaluated + s2
evaluated = evaluated + secret[-1]
return evaluated
letters = set()
partial = ''
def takeguess(ch):
global numguesses, points, games, wins, partial, guesses
numguesses = numguesses + 1
print 'Remaining Guesses: ' + str(maxguesses-numguesses)
if (ch in partial) or (ch in guesses):
print "\nYou've already guessed '%s'. Try again: " % ch
elif ch not in secret:
guesses = guesses + ch
print 'Wrong, guess again.'
elif ch in secret:
letters.add(ch)
print "\nGood guess, it's in the secret formula!"
partial = ''.join([l if l in letters else '-' for l in secret])
print 'Formula so far: ' + partial
if partial == secret:
print 'You win!'
wins = wins + 1
points = points + 2
games = games + 1
bonus = raw_input("Evaluate the formula for 10 bonus points: ")
if bonus == str(eval(evaluate(secret))):
points = points + 10
print "That's correct! +10 points."
print eval(evaluate(secret)) #REMOVE AFTER
play_again()
else:
print "That's incorrect. The right answer is", eval(evaluate(secret))
play_again()
def play_again():
global numguesses, guesses, partial, letters
letters = set()
if points < 2:
print "\nYou don't have enough points to play again.\nGames Played: " + str(games) + '\nPoints: ' + str(points)
else:
ans = raw_input('Would you like to play again? [y/n] ')
if ans in ('yY'):
numguesses = 0
guesses = ''
partial = ''
play()
elif ans in ('nN'):
print '\nOkay, goodbye.\nWins: ' + str(wins) + '\nLosses: ' + str(losses) + '\nPoints: ' + str(points)
else:
print 'Invalid input.'
play_again()
def play():
global numguesses, guesses, partial, points, games, losses
genform()
print 'Unsolved formula: ' + ('-'*len(secret))
print 'You have ' + str(maxguesses) + ' guesses.'
print secret #REMOVE AFTER
while (numguesses < maxguesses):
guess = raw_input('\nEnter a guess: ') # Receive guess
if guess not in allowed: # Check if valid
print '\nInvalid guess, please enter a single digit, *, or +. Try again: '
elif partial != secret:
takeguess(guess)
print guesses # REMOVE AFTER
if numguesses == maxguesses:
points = points - 2
games = games + 1
losses = losses + 1
print 'Sorry, you lose. The answer is: ' + secret
play_again()
return
print 'Welcome to hangman!\n'
play()