2016-11-06 6 views
-2

私はこの小さなゲームを作ろうとしています。何らかの理由で、「Y」と入力すると「あなたはリンゴを選んでみませんか?私は何をしようと、ITは1にとどまります。ここに私のコードはあります:なぜリンゴは増えていないのですか?

import time 
global choice 
global gold 
global apples 
apples = 0 
gold = 0 

def begin(): 
    apples = 0 
    gold = 0 
    print ("Let's go!") 
    if gold > 99: 
     print ("You've won the game!") 
     play = input ("Do you want to play again? Please answer Y/N.") 
     if play == "Y": 
      begin() 
     if play == "N": 
      print ("Okay, bye then.") 
    pick = input ("Do you want to pick an apple Y/N?") 
    if pick == "Y": 
     print ("You pick an apple") 
     apples=apples+1 
     apples = 1 
     print ("You currently have,",apples," apples") 
     begin() 
    if pick == "N": 
     sell = input("Do you want to sell your apples Y/N?") 
     if sell == "Y": 
      gold 
      apples 
      print ("You currently have,",apples,"apples") 
      print("You have sold your apples") 
      gold=apples*10 
      print ("Your gold is now:",gold) 
      begin() 
      start() 

print ("Hello and welcome!") 
name = input("What's your name?") 
print ("Welcome, "+name+"!") 
print ("The goal of this game is to collect apples") 
print ("After you have collected these applaes, you sell them.") 
choice = input("Do you want to play? Type Y/N.") 
if choice == "Y": 
    begin() 
if choice == "N": 
    print ("Okay, bye then.") 

誰でもこの問題を手伝うことができたら非常に感謝します。私はちょうど初心者なので、あまりにも厳しくはありません。この問題が明らかな場合は申し訳ありませんが、私はちょうど始めたばかりです。

答えて

0

行の後

リンゴ=リンゴ+ 1

は、あなたがそれはあなたが唯一の1を持っていることを表示させ、ラインに

りんご= 1にリンゴをリセット1

を持っています林檎。

0

コードの最上位にglobal文があります。それらは何もしません。グローバル変数を使用する場合は、変数を使用する関数の中にglobalステートメントを入れて、ローカル変数を使用せずにその名前のグローバル変数を使用するようにPythonに指示する必要があります。

試してみてください。

apples = 0 # don't repeat these lines inside the function 
gold = 0 # (unless you want the variables to get reset each time you call it) 

def begin(): 
    global gold # move the global statements inside the function 
    global apples 

    # ... 

choice変数はbegin機能で使用されていないようなので、あなたはそれのためglobal文は必要ありません。

Dobellyoが指摘したように、あなたはまた、機能内で​​へのあなたの割り当てにいくつかの混乱したロジックを持っています。既存の値をインクリメントするときと、固定値を代入するときを決定する必要があります。通常、両方を行うことは通常、どちらも意味をなさない。

関連する問題