0
import random

while True:
    dice1=random.randint (1,6)
    dice2=random.randint (1,6)


strengthone = input ("Player 1, between 1 and 10 What do you want your characters strength to be? Higher is not always better.")
skillone = input ("Player 1, between 1 and 10 What do you want your characters skill to be? Higher is not always better.")

if str(strengthone) > int(10):
    print ("Incorrect value")
else:
    print ("Good choice.")
if skillone > 10:
    print ("Incorrect value.")
else:
    print ("Good choice.")

strengthtwo = input ("Player 2, between 1 and 10 what do you want your characters strength to be? Higher is not always better.")
skilltwo = input ("Player 2, between 1 and 10 what do you want your characters skill to be? Higher is not always better.")

if strengthtwo > 10:
    print ("Incorrect value.")
else:
    print ("Good choice.")
if skillone > 10:
    print ("Incorrect value.")
else:
    print ("Good choice.")

strengthmod = strengthone - strengthtwo
skillmod = skillone - skilltwo

print ("Player 1, you rolled a", str(dice1))
print ("Player 2, you rolled a", str(dice2))



if dice1 == dice2:
    print ("")
if dice1 > dice2:
    newstrengthone = strengthmod + strengthone
    newskillone = skillmod + skillone
if dice2 > dice1:
    newstrengthtwo = strengthmod + strengthtwo
    newskilltwo = skillmod + skilltwo
if dice1 < dice2:
    newstrengthone = strengthmod - strengthone
    newskillone = skillmod - skillone
if dice2 < dice1:
    newstrengthtwo = strengthmod - strengthtwo
    newskilltwo = skillmod - skilltwo

if strengthone == 0:
    print ("Player one dies, well done player two. You win!")
if strengthtwo == 0:
    print ("Player two dies, well done player one. You win!")
if newstrengthone == 0:
    print ("Player one dies, well done player two. You win!")
if newstrengthtwo == 0:
    print ("Player two dies, well done player one. You win!")

break

学校のプロジェクト用なので、コードの目的はあまり意味がありません。インデントが原因で構文エラーが発生しました。私はこれを持っている今、それらをソートしました:

Traceback (most recent call last):
  File "N:\Computing\Task 3\Program.py", line 11, in <module>
    if str(strengthone) > int(10):
TypeError: unorderable types: str() > int()

何か案は?

4

2 に答える 2

2
  1. strとを比較することはできませんint。だから変える

    if str(strengthone) > int(10):
    

    if int(strengthone) > int(10):
    
  2. int10は既に int であるため、10 を に変換する必要はありません。

    print (type(10)) # <type 'int'>
    

    だから、これはこのように書くことができます

    if int(strengthone) > 10:
    
  3. さらに良いことに、から直接出てくる値inputを対応する型に変換できます

    strengthone = int(input ("Player 1,..."))
    skilltwo = int(input ("Player 2,..."))
    

    したがって、このような値を比較できます

    if strengthone > 10:
    
于 2013-11-13T12:10:20.263 に答える
0

変化する

if str(strengthone) > int(10):

if strengthone.isdigit() and int(strengthone) > 10:

于 2013-11-13T12:13:35.620 に答える