前提
google colaboratoryにて指スマ(いっせーので)ちゃんと書いてるはずなのに答えが違う理由がわからないので教えてください。
前の質問でglobalというのを知って使っているのですがもしかしたらそれのせいなのかもしれません。
初心者で簡単なコードで作ってます。(コードが長くおそらく短くする方法などがあるだろうけどとりあえずそこには触れないでください。)
実現したいこと
・1本と入力したらちゃんとCPUのところに足されるようにする。
・いっせーので(指スマ)として成り立たせる。
起こっていること
なぜかinputで入力して足すようにしてるのに足されない。(ゲームが成り立ってない)
次はCPUの番だよ。 何本出すか入力してね。 ゆびすま・・・(あげる本数)2 CPUは 0 本あげたよ。 CPUは 0 本といったよ。 当たったので1本のけるよ。
該当のソースコード
#randomのインポート import random #初めの指の本数の関数定義 n = 2 nm = 2 #指スマ関数の定義(自分の番)2-2,1-2 def yubisuma1(): global n print("最初に何本出すか入力してね。") people = int(input("ゆびすま・・・(あげる本数)")) print(" ") print("合計全員で何本あげるか予想してね。") human = int(input("ゆびすま・・・(みんなとの合計)")) bot = random.randint(0,2) if people + bot == human: print("CPUは", bot , "本あげたよ。") print("あたり。一本のけてね。") n -= 1 if people + bot != human: print("CPUは", bot , "本あげたよ。") print("残念。") #指スマ関数の定義(自分の番)2-1,1-1 def yubisuma2(): global n print("最初に何本出すか入力してね。") people = int(input("ゆびすま・・・(あげる本数)")) print(" ") print("合計全員で何本あげるか予想してね。") human = int(input("ゆびすま・・・(みんなとの合計)")) bot = random.randint(0,1) if people + bot == human: print("CPUは", bot , "本あげたよ。") print("あたり。一本のけてね。") n -= 1 if people + bot != human: print("CPUは", bot , "本あげたよ。") print("残念。") #指スマ関数の定義(CPUの番)2-2 def yubisuma5(): global nm print("次はCPUの番だよ。") print("") print("何本出すか入力してね。") people = int(input("ゆびすま・・・(あげる本数)")) print(" ") cpu = random.randint(0,4) bot = random.randint(0,2) if people + bot == cpu: print("CPUは", bot , "本あげたよ。") print("CPUは", bot , "本といったよ。") print("当たったので1本のけるよ。") nm -= 1 if people + bot != cpu: print("CPUは", bot , "本あげたよ。") print("CPUは", cpu , "本といったよ。") print("残念。") #指スマ関数の定義(CPUの番)2-1 def yubisum6(): global nm print("次はCPUの番だよ。") print("") print("何本出すか入力してね。") people = int(input("ゆびすま・・・(あげる本数)")) print(" ") cpu = random.randint(0,3) bot = random.randint(0,1) if people + bot == cpu: print("CPUは", bot , "本あげたよ。") print("CPUは", bot , "本といったよ。") print("当たったので1本のけるよ。") nm -= 1 if people + bot != cpu: print("CPUは", bot , "本あげたよ。") print("CPUは", cpu , "本といったよ。") print("残念。") #指スマ関数の定義(CPUの番)1-2 def yubisuma7(): global nm print("次はCPUの番だよ。") print("") print("何本出すか入力してね。") people = int(input("ゆびすま・・・(あげる本数)")) print(" ") cpu = random.randint(0,3) bot = random.randint(0,2) if people + bot == cpu: print("CPUは", bot , "本あげたよ。") print("CPUは", bot , "本といったよ。") print("当たったので1本のけるよ。") nm -= 1 if people + bot != cpu: print("CPUは", bot , "本あげたよ。") print("CPUは", cpu , "本といったよ。") print("残念。") #指スマ関数の定義(CPUの番)1-1 def yubisuma8(): global nm print("次はCPUの番だよ。") print("") print("何本出すか入力してね。") people = int(input("ゆびすま・・・(あげる本数)")) print(" ") cpu = random.randint(0,2) bot = random.randint(0,1) if people + bot == cpu: print("CPUは", bot , "本あげたよ。") print("CPUは", bot , "本といったよ。") print("当たったので1本のけるよ。") nm -= 1 if people + bot != cpu: print("CPUは", bot , "本あげたよ。") print("CPUは", cpu , "本といったよ。") print("残念。") #関数を動かす。 yubisuma1() while True: if n == 1 and nm == 2: print(" ") print(" ") yubisuma7() if n == 1 and nm == 2: print(" ") print(" ") yubisuma1() if n == 2 and nm == 2: print(" ") print(" ") yubisuma1() if n == 2 and nm == 1: print(" ") print(" ") yubisuma2() if n == 1 and nm == 1: print(" ") print(" ") yubisuma2() if n == 0: print(" ") print(" ") print("あなたの勝ち!") break if nm == 0: print(" ") print(" ") print("CPUの勝ち!") break if n == 2 and nm == 2: print(" ") print(" ") yubisuma5() if n == 1 and nm == 2: print(" ") print(" ") yubisuma1() if n == 2 and nm == 2: print(" ") print(" ") yubisuma1() if n == 2 and nm == 1: print(" ") print(" ") yubisuma2() if n == 1 and nm == 1: print(" ") print(" ") yubisuma2() if n == 0: print(" ") print(" ") print("あなたの勝ち!") break if nm == 0: print(" ") print(" ") print("CPUの勝ち!") break if n == 2 and nm == 1: print(" ") yubisuma6() if n == 1 and nm == 2: print(" ") print(" ") yubisuma1() if n == 2 and nm == 2: print(" ") print(" ") yubisuma1() if n == 2 and nm == 1: print(" ") print(" ") yubisuma2() if n == 1 and nm == 1: print(" ") print(" ") yubisuma2() if n == 0: print(" ") print(" ") print("あなたの勝ち!") break if nm == 0: print(" ") print(" ") print("CPUの勝ち!") break if n == 1 and nm == 1: print("") print(" ") yubisuma8() if n == 1 and nm == 2: print(" ") print(" ") yubisuma1() if n == 2 and nm == 2: print(" ") print(" ") yubisuma1() if n == 2 and nm == 1: print(" ") print(" ") yubisuma2() if n == 1 and nm == 1: print(" ") print(" ") yubisuma2() if n == 0: print(" ") print(" ") print("あなたの勝ち!") break if nm == 0: print(" ") print(" ") print("CPUの勝ち!") break if n == 0: print("") print("") print("あなたの勝ち!") break if nm == 0: print("") print("") print("CPUの勝ち!") break
不思議な点
・人間の番のほうはちゃんと足されてるのにCPUのほうがたされてない点
・エラーなどは出ず実行はできる点。
補足情報(FW/ツールのバージョンなど)
・google colaboratoryを使用してます
・最近global関数知って使ってます。
・結構時間かけて作ってるので是非教えてください;;
回答3件
あなたの回答
tips
プレビュー