始めている間、私は小さなプロジェクトとしてじゃんけんゲームを作っています、そして私は同じビットのコードをたくさん使わなければなりません。現時点では、コピーして貼り付けているので、これを行うための最良の方法は何ですか。乱雑に見えます。さらに、入力の検証中にループが発生する問題があり、すべてを変更する必要があるのは面倒です。
user_continue = raw_input("Would you like to play again? Y/N: ")
user_continue = user_continue.upper()
#Yes or no continue for the continue while loop to continue working until correct user input.
y_n_continue = False
while y_n_continue == False:
if user_continue == "Y" or user_continue == "YES" or user_continue == "N" or user_continue == "NO":
if user_continue == "Y" or user_continue == "YES":
continue_game = True
y_n_continue = True
elif user_continue == "N" or user_continue == "NO":
continue_game = False
y_n_continue = True
else:
print "Press Y or N"
y_n_continue = False
else:
print ""
コード全体を追加した方がおそらく簡単でしょう(Antonのおかげで、修正が加えられました。現時点では、エラーが発生しています-TypeError:'bool'オブジェクトは呼び出せません。
私は基本的に、ユーザーが望む限りゲームをループさせ、入力を検証してすべてを可能な限り防弾にするようにしています。
編集2-これが新しいコードで、その下にいくつかのテストデータがあります。
起動すると、最初にy/nを入力するように求められます。
また、各ゲームの後にyまたはnを2回入力する必要があります。
じゃんけんの選択に「間違った」データを入力すると、y/nの選択になります
import random
def continue_game():
while True:
user_continue = raw_input("Would you like to play again? Y/N: ").upper()
if user_continue in ["Y", "YES", "N", "NO"]:
return user_continue in ["Y", "YES"]
else:
print "Press Y or N"
while continue_game():
#computers choice of rock, paper or scissors
computer_input = ["ROCK", "PAPER", "SCISSORS"]
computer_choice = random.choice(computer_input)
#users choice or rock, paper or scissors
user_input = raw_input("Choose rock, paper or scissors: ")
#Turns user input to upper case.
user_choice = user_input.upper()
if user_choice == "ROCK" or user_choice == "PAPER" or user_choice == "SCISSORS":
#Computer = ROCK
if computer_choice == "ROCK":
#user = ROCK
if user_choice == "ROCK":
print "You have chosen: " + user_choice
print "The computer has chosen: " + computer_choice
print "You draw!"
#replay?
if continue_game():
print "continue"
else:
continue_game = False
#user = PAPER
elif user_choice == "PAPER":
print "You have chosen: " + user_choice
print "The computer has chosen: " + computer_choice
print "You win!"
#replay?
if continue_game():
print "continue"
else:
continue_game = False
#user = SCISSORS
elif user_choice == "SCISSORS":
print "You have chosen: " + user_choice
print "The computer has chosen: " + computer_choice
print "You lose!"
#replay?
if continue_game():
print "continue"
else:
continue_game = False
#Computer = PAPER
elif computer_choice == "PAPER":
#user = ROCK
if user_choice == "ROCK":
print "You have chosen: " + user_choice
print "The computer has chosen: " + computer_choice
print "You lose!"
#replay?
if continue_game():
print "continue"
else:
continue_game = False
#user = PAPER
elif user_choice == "PAPER":
print "You have chosen: " + user_choice
print "The computer has chosen: " + computer_choice
print "You draw!"
if continue_game():
print "continue"
else:
continue_game = False
#user = SCISSORS
elif user_choice == "SCISSORS":
print "You have chosen: " + user_choice
print "The computer has chosen: " + computer_choice
print "You win!"
#replay?
if continue_game():
print "continue"
else:
continue_game = False
#Computer = SCISSORS
elif computer_choice == "SCISSORS":
#user = ROCK
if user_choice == "ROCK":
print "You have chosen: " + user_choice
print "The computer has chosen: " + computer_choice
print "You win!"
#replay?
if continue_game():
print "continue"
else:
continue_game = False
#user = PAPER
elif user_choice == "PAPER":
print "You have chosen: " + user_choice
print "The computer has chosen: " + computer_choice
print "You lose!"
#replay?
if continue_game():
print "continue"
else:
continue_game = False
#user = SCISSORS
elif user_choice == "SCISSORS":
print "You have chosen: " + user_choice
print "The computer has chosen: " + computer_choice
print "You draw!"
#replay?
if continue_game():
print "continue"
else:
continue_game = False
else:
print "Something has gone wrong."
else:
print "Are you sure you entered that correctly?"
出力:
Would you like to play again? Y/N: y
Choose rock, paper or scissors: rock
You have chosen: ROCK
The computer has chosen: PAPER
You lose!
Would you like to play again? Y/N: y
continue
Would you like to play again? Y/N: y
Choose rock, paper or scissors: paper
You have chosen: PAPER
The computer has chosen: ROCK
You win!
Would you like to play again? Y/N: wer
Press Y or N
Would you like to play again? Y/N: dfg
Press Y or N
Would you like to play again? Y/N: y
continue
Would you like to play again? Y/N: y
Choose rock, paper or scissors: test
Are you sure you entered that correctly?
Would you like to play again? Y/N: y
Choose rock, paper or scissors: rock
You have chosen: ROCK
The computer has chosen: SCISSORS
You win!
Would you like to play again? Y/N: n
exit
Would you like to play again? Y/N: n
>>>
私は自分が苦しんでいることを知っていますが、これはすべてありがたいことです。