2016-10-17 7 views
-1

私のコードにいくつかの問題があります。私は変化の最大の下で5の倍数で入力を制限する必要があります。pythonの計算が奇妙です

私が今使っているものではうまくいくはずですが、そうではありません。私は何を逃したのですか?

while True: 
     print("Place your bet. Must be a multiple of 5") 
     posBets = range(5, (balance + 1), 5) 
     bet = str(input()) 
     if bet == posBets: 
      print("You have bet $" + str(bet)) 
      loop = 0 
     elif bet != posBets: 
       print("Please bet an amount that is a multiple of 5, or an amount that is within your balance") 
       print("Press enter to continue") 
       input() 
       loop = 1 
     if not(loop == 1):break #Exit loop 

ループにも問題があります。イムは、私の新しい値を取得しようとすると、それが元の値から減算し、main関数に戻ってそれを送信する必要があるしかし、それは常に、それがここにあるメイン

win = 1 
lose = 2 
tie = 3 
r = win or lose or tie 
print("Battle shall now commence") 
print("Choose an attack") 
print("The attacks you can use are as follows") 
print("(1)Fury Punch") 
print("(2)Punishment kick") 
print("(3)Sword of justice") 
print("(4)Shuriken of Vengence") 
print("(5)Numchucks of Anger") 
print("(6)Knife of Freedom") 
attack = int(input()) 
while attack < 1 or attack > 6: 
    print("please input either: 1, 2, 3, 4, 5 or 6") 
    attack = int(input()) 
winLose(attack, win, lose, tie,) 
if r == win: 
    newBalance = int(balance) + int(bet) 
elif r == lose: 
    newBalance = int(balance) - int(bet) 
else: 
    if r == tie: 
     balance = newBalance 
print("Your Balance is $" + str(newBalance)) 
return(bet, newBalance) 

に送信する値を変更doenst、それを追加します完全なコード

def attack1(pcMove, win, tie, lose): 
if pcMove == 1: 
    print("Your opponent has choosen Fury Punch") 
    print("it is a tie there is no winner") 
    r = tie 
elif pcMove == 2: 
    print("Your opponent has choosen Punishment Kick") 
    print("You have lost") 
    r = lose 
elif pcMove == 3: 
    print("Your opponent has choosen Sword of Justice") 
    print("You have lost") 
    r = lose 
elif pcMove == 4: 
    print("Your opponent has choosen Shuriken of Vengeance") 
    print("You have won. Good job") 
    r = win 
elif pcMove == 5: 
    print("Your opponent has choosen Numchucks of Anger") 
    print("You have won. Good job") 
    r = win 
else: 
    print("Your opponent has choosen Knife of Freedom") 
    print("You have lost") 
    r = lose 
return(r) 

def attack2(pcMove, win, tie, lose): 
if pcMove == 1: 
    print("Your opponent has choosen Fury Punch") 
    print("You have won. Good job") 
    r = win 
elif pcMove == 2: 
    print("Your opponent has choosen Punishment Kick") 
    print("it is a tie there is no winner") 
    r = tie 
elif pcMove == 3: 
    print("Your opponent has choosen Sword of Justice") 
    print("You have lost") 
    r = lose 
elif pcMove == 4: 
    print("Your opponent has choosen Shuriken of Vengeance") 
    print("You have lost") 
    r = lose 
elif pcMove == 5: 
    print("Your opponent has choosen Numchucks of Anger") 
    print("You have won. Good job") 
    r = win 
else: 
    print("Your opponent has choosen Knife of Freedom") 
    print("You have lost") 
    r = lose 
return(r) 

def attack3(pcMove, win, tie, lose): 
if pcMove == 1: 
    print("Your opponent has choosen Fury Punch") 
    print("You have won. Good job") 
    r = win 
elif pcMove == 2: 
    print("Your opponent has choosen Punishment Kick") 
    print("You have won. Good job") 
    r = win 
elif pcMove == 3: 
    print("Your opponent has choosen Sword of Justice") 
    print("it is a tie there is no winner") 
    r = tie 
elif pcMove == 4: 
    print("Your opponent has choosen Shuriken of Vengeance") 
    print("You have lost") 
    r = lose 
elif pcMove == 5: 
    print("Your opponent has choosen Numchucks of Anger") 
    print("You have lost") 
    r = lose 
else: 
    print("Your opponent has choosen Knife of Freedom") 
    print("You have won. Good job") 
    r = win 
return(r) 

def attack4(pcMove, win, tie, lose): 
if pcMove == 1: 
    print("Your opponent has choosen Fury Punch") 
    print("You have lost") 
    r = lose 
elif pcMove == 2: 
    print("Your opponent has choosen Punishment Kick") 
    print("You have won. Good job") 
    r = win 
elif pcMove == 3: 
    print("Your opponent has choosen Sword of Justice") 
    print("You have won. Good job") 
    r = win 
elif pcMove == 4: 
    print("Your opponent has choosen Shuriken of Vengeance") 
    print("it is a tie there is no winner") 
    r = tie 
elif pcMove == 5: 
    print("Your opponent has choosen Numchucks of Anger") 
    print("You have lost") 
    r = lose 
else: 
    print("Your opponent has choosen Knife of Freedom") 
    print("You have won. Good job") 
    r = win 
return(r) 

def attack5(pcMove, win, tie, lose): 
if pcMove == 1: 
    print("Your opponent has choosen Fury Punch") 
    print("You have lost") 
    r = lose 
elif pcMove == 2: 
    print("Your opponent has choosen Punishment Kick") 
    print("You have lost") 
    r = lose 
elif pcMove == 3: 
    print("Your opponent has choosen Sword of Justice") 
    print("You have won. Good job") 
    r = win 
elif pcMove == 4: 
    print("Your opponent has choosen Shuriken of Vengeance") 
    print("You have won. Good job") 
    r = win 
elif pcMove == 5: 
    print("Your opponent has choosen Numchucks of Anger") 
    print("it is a tie there is no winner") 
    r = tie 
else: 
    print("Your opponent has choosen Knife of Freedom") 
    print("You have lost") 
    r = lose 
return(r) 

def attack6(pcMove, win, tie, lose): 
if pcMove == 1: 
    print("Your opponent has choosen Fury Punch") 
    print("You have won. Good job") 
    r = win 
elif pcMove == 2: 
    print("Your opponent has choosen Punishment Kick") 
    print("You have won. Good job") 
    r = win 
elif pcMove == 3: 
    print("Your opponent has choosen Sword of Justice") 
    print("You have lost") 
    r = lose 
elif pcMove == 4: 
    print("Your opponent has choosen Shuriken of Vengeance") 
    print("You have lost") 
    r = lose 
elif pcMove == 5: 
    print("Your opponent has choosen Numchucks of Anger") 
    print("You have won. Good job") 
    r = win 
else: 
    print("Your opponent has choosen Knife of Freedom") 
    print("it is a tie there is no winner") 
    r = tie 
return(r) 

def winLose(attack, win, tie, lose): 
import random 
pcMove = random.randint(1, 6); 
while pcMove == 0: 
    pcMove = random.randint(1, 6); 
if attack == 1: 
    print("You have choosen to use Fury Punch. Good Luck") 
    print("Ready for battle. Press enter to start") 
    enter = input() 
    attack1(pcMove, win, tie, lose) 
    r = win or lose or tie 
elif attack == 2: 
    print("You have choosen to use Punishment Kick. Good Luck") 
    print("Ready for battle. Press enter to start") 
    enter = input() 
    attack2(pcMove, win, tie, lose) 
    r = win or lose or tie 
elif attack == 3: 
    print("You have choosen to use Sword of Justice. Good Luck") 
    print("Ready for battle. Press enter to start") 
    enter = input() 
    attack3(pcMove, win, tie, lose) 
    r = win or lose or tie 
elif attack == 4: 
    print("You have choosen to use Shuriken of Vengence. Good Luck") 
    print("Ready for battle. Press enter to start") 
    enter = input() 
    attack4(pcMove, win, tie, lose) 
    r = win or lose or tie 
elif attack == 5: 
    print("You have choosen to use Numchucks of Anger. Good Luck") 
    print("Ready for battle. Press enter to start") 
    enter = input() 
    attack5(pcMove, win, tie, lose) 
    r = win or lose or tie 
else: 
    print("You have choosen to use Knife of Freedom. Good Luck") 
    print("Ready for battle. Press enter to start") 
    enter = input() 
    attack6(pcMove, win, tie, lose) 
    r = win or lose or tie 
return(r) 

def instructions(name): 
print("Welcome to Ultimate Ninja Combat!!!" + name) 
print("You will be playing against the computer, and the winner gets bragging rights. Before each match you will have to place a bet which must be an amount which must be a multiple of 5. if you win you get that amount back from the computer. if you lose you lose the money. if your amount drops to zero you will be removed from the game") 
print("you will start with 100 dollars use it wisely") 
print("Each turn you will be asked to pick one of the following attacks") 
print("(1)Fury Punch") 
print("(2)Punishment kick") 
print("(3)Sword of justice") 
print("(4)Shuriken of Vengence") 
print("(5)Numchucks of Anger") 
print("(6)Knife of Freedom") 
print("choose wisely") 
print(" ") 
print(" ") 
print(" ") 

def gameplay(balance): 
while True: 
    print("Place your bet. Must be a multiple of 5") 
    posBets = range(5, (balance + 1), 5) 
    bet = str(input()) 
    if bet == posBets: 
     print("You have bet $" + str(bet)) 
     loop = 0 
    elif bet != posBets: 
      print("Please bet an amount that is a multiple of 5, or an amount that is within your balance") 
      print("Press enter to continue") 
      input() 
      loop = 1 
    if not(loop == 1):break #Exit loop 
    return(bet) 
win = 1 
lose = 2 
tie = 3 
r = win or lose or tie 
print("Battle shall now commence") 
print("Choose an attack") 
print("The attacks you can use are as follows") 
print("(1)Fury Punch") 
print("(2)Punishment kick") 
print("(3)Sword of justice") 
print("(4)Shuriken of Vengence") 
print("(5)Numchucks of Anger") 
print("(6)Knife of Freedom") 
attack = int(input()) 
while attack < 1 or attack > 6: 
    print("please input either: 1, 2, 3, 4, 5 or 6") 
    attack = int(input()) 
winLose(attack, win, lose, tie,) 
if r == win: 
    newBalance = int(balance) + int(bet) 
elif r == lose: 
    newBalance = int(balance) - int(bet) 
else: 
    if r == tie: 
     balance = newBalance 
print("Your Balance is $" + str(newBalance)) 
return(bet, newBalance) 

def menu(name): 
    balance = 100 
    while True: 
     newBalance = balance 
     print("Your current balance is $" + str(balance)) 
     print("Choose an option " + name) 
     print("(I)nstructions") 
     print("(P)lay game") 
     print("(Q)uit game") 
     print("Please Input I,P or Q") 
     menuChoice = input()    
     if menuChoice == "i" or menuChoice == "I": 
      instructions(name) 
      loop = 1 
     elif menuChoice == "p" or menuChoice == "P": 
      gameplay(balance) 

      loop = 1 
      print("Press enter to contiue") 
      enter = input() 
     elif menuChoice == "q" or menuChoice == "Q": 
      loop = 0 
     else:     
      print("I did not understand the response. Please Input I, P or Q") 
      print("Press enter to continue") 
      enter = input() 
      loop = 1 
     if not(loop == 1): break #Exit loop 
    return(menuChoice) 

def main(): 
print("Welcome to Ultimate Ninja Combat!!! What is your name?") 
name = input() 
print ("Welcome " + name) 
menu(name) 
メイン

()

+2

あなたのコード全体を投稿しないようにしてください。あなたにエラーが出ると思われるビット –

答えて

2

あなたbet = str(input())はそれが範囲を等しくことはできません理由は、文字列の中に数値を変換任意の助けをありがとうございました。

intであっても、数値は範囲に一致しません。

何がしたいことは

bet = int(input()) 
    if bet in posBets: 

あるコードの残りの部分は私にはエラーの完全なようだ:defのようなものを言って

  • をINEDとき

    • 機能はインデントする必要がありますと言った後にwin=1lose=2tie=3が基本的に質問していますがTrueとして評価しorは、すぐにそれがTrueを取得するように停止するので、常に1ある(それはFalseを取得する場合だけandのように停止します)

  • 0

    それは等しくすることはできませんので、あなたは文字列に整数に変換されています範囲もまた整数では決して最初の範囲の範囲には決してならないので、その機能の仕方を変更する必要があります..

    'str'の代わりに 'int'に変更するだけで整数に変換できます:

    bet = int(input()) 
    
    +0

    この回答はどのように既存の回答に寄与していますか? –