私はPythonで退屈なものを自動化することでPythonを学んでいます。最初の章を進めていったので、作者の目的とは異なるコードを作成することにしました。毒物学の授業中に、先生はある種のカタツムリが年間最大12%死んでいると言いました。そこで、それを計算するコードを作ることにしました。
私が作成したコードは次のとおりです。
number= input('Number of individuals: ') #tells the number of individuals in the beggining
count = 0 #tells the nuumber of generations
number=int(number)
percent=input('% of loss per generation ') #% of individuals lost in each generation
percent=int(percent)
#while number > 1:
# number= number*(percent*0.01)
# number=round(number,-1)
# count = count +1
#print('generation ' + str(count) + ':' + str(number*0.1) + ' individuals')
#if number == 1:
# print(' it took ' + str(count) + ' generations to end with the species')
while True:
if number == 1:
print(' it took ' + str(count) + ' generations to end with the species')
if number >1:
number= number*(percent*0.01)
number=round(number,-1)
count = count +1
print('generation ' + str(count) + ':' + str(number*0.1) + ' individuals')
if number == 0 :
break
私が作成しようとした最初のコードはコメントにあり、問題を解決するために別の方法を作成しようとしました。
私の問題は、個人の数と損失の割合を入力したいということです。最後に(個体が自分自身と交尾できないため1)、種で終わるまでにかかった世代数を教えてほしい。問題は、プログラムが想定よりも1回ループすると(そしてround()関数を使用する前はさらにループする)、0の値が必要ないために始まり、1に達したときにそれが必要になるという事実があります。それで終わるのに何世代かかったか教えてください。
私は答えを探そうとしましたが、私は初心者なので、適切な検索を行うための用語を正しく知らない可能性があります。私の見方では、プログラムは思ったように実行されているはずですが、私が得ていないものが欠けていると思います。
コメントでコードについて説明しましょう。これは、いくつかの改善が必要な、より優れたソリューションだからです。コードには2つの主な問題があります
1)なぜあなたnumber * 0.1
はただではなく印刷するのnumber
ですか?10を表示する必要があるときに1を表示します。これが主な問題であり、誤解を招くと思います。
print('generation ' + str(count) + ':' + str(number*0.1) + ' individuals')
2)次の2行はループ内にない必要があります。これは、実際には1を通過してundに結果メッセージが表示されない可能性があるためです。これらの行は、ループの後に配置する必要があります。
if number == 1:
print(' it took ' + str(count) + ' generations to end with the species')
結果としてここに私たちが持っているものがあります:
number= input('Number of individuals: ') # tells the number of individuals in the beggining
count = 0 # tells the nuumber of generations
number=int(number)
percent=input('% of loss per generation ') # % of individuals lost in each generation
percent=int(percent) * 0.01
while number > 1:
number = number * percent
number = round(number)
count = count +1
print('generation ' + str(count) + ':' + str(number) + ' individuals')
print(' it took ' + str(count) + ' generations to end with the species')
更新:
そして、あなたが気にしないのであれば、いくつかのマイナーなコードスタイルの改善コードが適用されます:
更新#2:
ループだけを修正しましたが、@ Alanがコメントで指摘したように、損失の計算が間違っています。パーセントは1 - int(percent) * 0.01
number = input('Number of individuals: ') # tells the number of individuals in the beggining
number = int(number)
percent = input('% of loss per generation ') # % of individuals lost in each generation
percent = 1 - int(percent) * 0.01
count = 0 # tells the nuumber of generations
while number >= 2:
number = number * percent
count += 1
print('generation {}: {} individuals'.format(count, round(number)))
print(' it took {} generations to end with the species.'.format(count))
コードhttps://repl.it/NWi4/2を試してください
更新#3:
丸めを改善するためにコードブロックを変更しました。それはround(number, -1)
、数十の丸めにつながった。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加