2010-12-27 9 views
0

私はPythonのnoobです。私は 'Python Programming Absolute Beginner(第2版 - Python 2.3、しかし私は2.7を使用しています)'を介してPythonを理解しています。絶対初心者のためのPythonプログラミング

本は完全な課題を提示しています。私が上に移動する前に、私はこの周りに頭を浮かべたいので、どんな助けでも大いに感謝されます。

第3章チャレンジ3 - 推測マイナンバー:プレイヤーが数字を推測する回数を制限するために、以下のコードを修正してください。

どうすればいいですか?変数を設定するために私がこれまで行ってきた試みは、ユーザーが答えを得ているかどうかにかかわらず答えがすべて消えてしまった。先輩に感謝します。

プレイヤーはそれを推測しようとする>コンピュータは、1と100の間の乱数を選ぶマイナンバー

を推測し、 コンピュータは推測が右に低すぎたり、高すぎるプレイヤーは知ることができますお金

import random 

print "\tWelcome to 'Guess My Number'!" 
print "\nI'm thinking of a number between 1 and 100." 
print "Try to guess it in as few attempts as possible.\n" 

# set the initial values 
the_number = random.randrange(100) + 1 
guess = int(raw_input("Take a guess: ")) 
tries = 1 

# guessing loop 
while (guess != the_number): 
    if (guess > the_number): 
     print "Lower..." 
    else: 
     print "Higher..." 

guess = int(raw_input("Take a guess: ")) 
tries += 1 

print "You guessed it! The number was", the_number 
print "And it only took you", tries, "tries!\n" 

raw_input("\n\nPress the enter key to exit.") 

はこれまでのところ、私は失敗し、次の試行されてきました。

import random 

print "\tWelcome to 'Guess My Number'!" 
print "\nI'm thinking of a number between 1 and 100." 
print "Try to guess it in as few attempts as possible.\n" 

# set the initial values 
the_number = random.randrange(100) + 1 
guess = int(raw_input("Take a guess: ")) 
tries = 1 
limit = 8 

# guessing loop 
while (guess != the_number and tries < limit): 
    if (guess > the_number): 
     print "Lower..." 
    elif (guess < the_number): 
     print "Higher..." 
    else: 
     print "You've used all " + limit -1 +"of your attempts \ 
and didn't get the right answer. Shame on You!" 

    guess = int(raw_input("Take a guess: ")) 
    tries += 1 

print "You guessed it! The number was", the_number 
print "And it only took you", tries, "tries!\n" 

raw_input("\n\nPress the enter key to exit.") 
+4

元のコードの仕組みを自分の言葉で説明してみませんか? –

+1

申し訳ありませんが、私は持っていたと思いました。それは単純なゲームです。「推測するマイナンバー - コンピュータが1から100の間のランダムな数字を選ぶ」プレイヤーは推測しようとします。推測が高すぎたり、低すぎたり、私はここで新しいです、私はさらに説明しなければなりませんか? – Kurt

答えて

1

私はこの練習が教えたいと思うのはbreakです。 1つの終了条件(数値を推測する)がある前に、試行回数も制限されました。これを行うには

1つの直線方法:

import random 

print "\tWelcome to 'Guess My Number'!" 
print "\nI'm thinking of a number between 1 and 100." 
print "Try to guess it in as few attempts as possible.\n" 

# set the initial values 
the_number = random.randrange(100) + 1 

limit = 5 
tries = 0 

# guessing loop 
while True: # we will test the conditions separately in the loop, not here 
    # take a guess 
    guess = int(raw_input("Take a guess: ")) 
    tries += 1 

    # first check the number 
    if (guess > the_number): 
     print "Lower..." 
    elif (guess < the_number): 
     print "Higher..." 
    else: # it can only be equal here 
     print "You guessed it! The number was", the_number 
     print "And it only took you", tries, "tries!\n" 
     break # exit the while loop 

    # now the tries: 
    if tries == limit: 
     print "You've used all %d of your attempts \ 
and didn't get the right answer. Shame on You!" % limit 
     break 

raw_input("\n\nPress the enter key to exit.") 
+1

THC4k、これは非常に細かいことであり、よくコメントされています。非常に高く評価。ありがとうございました。 – Kurt

2

あなたには変数が試されています。どのようにしばらくの内側にそれをチェックし、それが一定値に達した場合、ユーザーと出口へのメッセージをプリントについて;)このような

+0

サンプルを投稿できますか?私は今まで試みたことを投稿します。 – Kurt

+0

おそらくそれはペーストですが、上のインデントに問題があります。あなたがwhileループの一部となるように内側のタブをインデントした場合、次のように変更することができます: while(推測!= the_number): 試してみると:> "もう試してください" ここから出るか、好きなことをしてください。 – Spyros

+0

SpyrosPはまさにそれでした。ありがとうございました。 whileループのraw_input部分を作成し、その結果が真であれば終了するようにしました。そのような簡単な解決策!みんなありがとう。私はまだ学んでいますが、うまくいけば私は恩恵を返すことができ、将来いくつかの騒動を助けることができます。 – Kurt

0

何か:

import random 

print "\tWelcome to 'Guess My Number'!" 
print "\nI'm thinking of a number between 1 and 100." 
print "Try to guess it in as few attempts as possible.\n" 

# set the initial values 
the_number = random.randrange(100) + 1 
guess = int(raw_input("Take a guess: ")) 
tries = 1 
max_tries = 5 

# guessing loop 
while ((tries < max_tries) && (guess != the_number)): 
    if (guess > the_number): 
     print "Lower..." 
    else: 
     print "Higher..." 
    tries += 1 
    guess = int(raw_input("Take a guess: ")) 

if tries == max_tries: 
    print "Max # of tries exceeded" 
else: 
    print "You guessed it! The number was", the_number 
    print "And it only took you", tries, "tries!\n" 

raw_input("\n\nPress the enter key to exit.") 
+0

ありがとうduffymo。問題は、max_triesを超えても答えが明らかになることです。どのようにそれを防ぐためのアイデア? – Kurt

+0

最後の2行をelseの中に入れてください: – jgritty

0

をあなたが推測周りにループを持っていませんこれは、推測が一度しか行われないことを意味します。推測とテストの構造全体を何らかのループ(forまたはwhile)の中に入れる必要があります。

0

あなたの試みは、「ほとんど正しいか私が見ることができる二つの問題ここでは最初の嘘があります。。

print "You guessed it! The number was", the_number 
print "And it only took you", tries, "tries!\n" 

はあなたが最大の試行が超えないた場合にのみ、上記を実行することができる方法を考えてみて。ヒント:あなたが1行を追加し、上記の二つのラインに微調整を加える必要があり

目はここにある:

else: 
     print "You've used all " + limit -1 +"of your attempts \ 
and didn't get the right answer. Shame on You!" 

ブロックelseブロックが実行されますか?理由を確認するには、whileの状態を振り返ります。繰り返しますが、これを修正する簡単な方法があります。

+0

ええ、noob間違いです。私はduffmoのコードサンプルに行って、単純に 'raw_input( "\ n \ n終了キーを押すとwhileループの一部として")'を含めました。よく働く。しかし、あなたの助けをありがとう。私は2つの条件を別々にテストできることを実感します。 – Kurt

1

私はちょうど同じ本を使用して、同様のプログラミングとPythonで始めています。ここで私が思いついたのは、それは動作するようです!

# Guess My Number 
# 
# The computer picks a random number between 1 and 100 
# The player tries to guess it and the computer lets 
# the player know if the guess is too high, too low 
# or right on the money 

import random 

print("\tWelcome to 'Guess My Number'!") 
print("\nI'm thinking of a number between 1 and 100.") 
print("Try to guess it in as few attempts as possible.\n") 

# set the initial values 
the_number = random.randint(1, 100) 
guess = int(input("Take a guess: ")) 

tries = 1 
max_tries = 8 

# guessing loop 
while tries < max_tries and guess != the_number: 

    if guess > the_number: 
     print("Lower...") 
    elif guess < the_number: 
     print("Higher...") 

    guess = int(input("Take a guess: ")) 
    tries += 1 

# having it stop if person uses maximum number of tries 
if tries >= max_tries: 

    print("Ruh roh. Looks like you're finished, pardner. The number was", the_number) 

# text if person gets the right number within the number of tries. 
else: 
    if guess == the_number: 
     print("You guessed it! The number was", the_number) 
     print("And it only took you", tries, "tries!\n") 

input("\n\nPress the enter key to exit.") 
関連する問題