前提
前回、長すぎてアドバイスをいただき短くしました!
ただ、このコードだとCPUの番の際ランダムでコンピューターが出す本数よりも合計のほうが小さいということが発生します。その解消の方法を考えてたんですがいい方法が思いつきません;; 答えでなくてもアドバイスでも全然いいので教えてください。
実現したいこと
・CPUの番の際ランダムでコンピューターが出す本数よりも合計のほうが必ず大きくする)
・なんでもいいのでアドバイス(もっと短くする方法があれば)
該当のソースコード
#関数のインポート import random import time #指の本数の定義 people_fin = 2 com_fin = 2 #関数作成 def people_yubisuma(): global people_fin global com_fin print("あなたの番!") print("何本出すか入力してね。") people_stand = int(input("あげる本数")) print(" ") print("合計全員で何本あげるか予想してね。") people_all = int(input("みんなとの合計")) print("ゆびすま、、") time.sleep(1) com_stand = random.randint(0, com_fin) if com_stand + people_stand == people_all: print("CPUは", com_stand, "本あげたよ。") print("あたり。一本のけてね。") people_fin -= 1 if com_stand + people_stand != people_all: print("CPUは", com_stand, "本あげたよ。") print("残念。") def com_yubisuma(): global people_fin global com_fin print("CPUの番!") print("何本出すか入力してね。") people_stand_up = int(input("あげる本数")) print("ゆびすま、、") time.sleep(1) com_stand_up = random.randint(0, com_fin) com_all = random.randint(0, com_fin + people_fin) if com_stand_up + people_stand_up == com_all: print("CPUは", com_stand_up, "本あげたよ。") print("CPUは", com_all, "本といったよ。") print("あたので一本のけるよ。") com_fin -= 1 if com_stand_up + people_stand_up != com_all: print("CPUは", com_stand_up, "本あげたよ。") print("CPUは", com_all, "本といったよ。") print("残念。") #関数動かす while True: print(" ") print(" ") people_yubisuma() if people_fin != 0 and com_fin != 0: print(" ") print(" ") com_yubisuma() if people_fin == 0: print(" ") print(" ") print("あなたの勝ち!") 3 break if com_fin == 0: print(" ") print(" ") print("CPUの勝ち!") break if people_fin == 0: print(" ") print(" ") print("あなたの勝ち!") break if com_fin == 0: print(" ") print(" ") print("CPUの勝ち!") break
補足情報(FW/ツールのバージョンなど)
googlecolaboratory使用。
anacondaでも大丈夫です(多分)。
回答1件
あなたの回答
tips
プレビュー