-2

これがコードです...

import random
Rock = '1'
Paper = '2'
Scissors = '3'
print('Welcome to Rock, Paper Scissors! The game of all kids to decide on something. \nIn this game you will have to beat the computer once. \n(Psst if it\'s a draw the start the program again! ;D)\nSo how to play. Well, it\'s simple. Pick 1 for Rock, 2 for Paper and 3 for Scissors. \nSo Rock beats Scissors. Scissors cuts Paper and Paper covers Rock. Got it Lets play')
player=int(input('Please enter number 1 = Rock, 2 = Paper, 3 = Scissors: '))
if player<1 or player>3 except player==9:
   player=int(input('Invalid number. Please enter number 1 = Rock, 2 = Paper, 3 = Scissors: '))
   if player<1 or player>3:
       print('Well, now you can\'t play this game because you are mucking around. Next time DON\'T!')
elif player==9:
    exit()
else:
   computer=random.randint(1, 3)
   print(player,computer)
   print('Remember Rock = 1, Paper = 2 and Scissors = 3')
   if player==1 and computer==1 or player==2 and computer==2 or player==3 and computer==3:
       print('It\'s a draw. =l Restart the game if you want to.')
   if player==1 and computer==2 or player==2 and computer==3 or player==3 and computer==1:
       print('Computer wins! You lose. Sorry. =(')
   if player==1 and computer==3 or player==2 and computer==1 or player==3 and computer==2:
       print('You have won. Well done. =D')

ユーザーが間違った番号を入力したときに、ユーザーが正しく応答するまで再度尋ねられるように、ループする必要があります。また、ユーザーが9を押して終了するまで、プログラムをループして再生する必要があります。

4

4 に答える 4

3

ループを使用すると、ゲームがいつ終了するかを制御できます。

game_over = False
while not game_over:
    do_stuff()
    if some_condition:
        game_over = True

ループの終わりに、ゲームが終了したかどうかをチェックします。そうである場合、ループは終了し、ループが実行された後のコードはすべて終了します。次に、プログラムが存在します。

于 2013-01-01T20:32:38.783 に答える
1

これをループさせ続けるためにできる最小の変更は、while Trueステートメントを追加するだけで、ゲームの残りの部分をその下にネストすることです。私はまた、そのように出口の場合のあなたのテストを修正します...

...
print('Welcome to Rock, Paper Scissors! ...')
while True:  # loop forever
  player=int(input('Please enter number 1 = Rock, 2 = Paper, 3 = Scissors: '))
  if player==9:  # but exit if 9
      exit()
  elif player<1 or player>3:
     ...
  else:
     ...
于 2013-01-01T20:34:09.900 に答える
1

このようなループのように単純なはずです。

while True:
  while player not in (1,2,3,9):
    keep_asking_for_input()
  if player == 9:
    break
  pass
于 2013-01-01T20:35:31.567 に答える
0

defとclassesを使用して、コードを書き直します。それははるかに簡単でしょう。whileループが必要です。

私はあなたのために完全なゲームを一から書きました。ここ:http ://codepad.org/Iy6YFW0H

コードを読んで、それがどのように機能するかを理解してください。お気軽にご利用ください。ちなみに、Python 2.xでは(申し訳ありませんが、Python 3は使用していません)。

于 2013-01-01T21:52:17.343 に答える