为什么我得到无序类型

2024-09-28 22:25:18 发布

您现在位置:Python中文网/ 问答频道 /正文

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

这是一个学校项目,所以代码的目标没有多大意义。我有一些语法错误,因为缩进。我把它们分类了现在我有了这个:

^{pr2}$

有什么想法吗?在


Tags: andyouinputifbetweendoplayerprint
2条回答

改变

if str(strengthone) > int(10):

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

  1. 不能比较str和{}。所以改变

    if str(strengthone) > int(10):
    

    if int(strengthone) > int(10):
    
  2. 而将10转换为int是不必要的,因为它已经是一个int

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

    所以,可以这样写

    if int(strengthone) > 10:
    
  3. 更好的是,您可以将input中的值转换为相应的类型

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

    所以,你可以像这样比较这些值

    if strengthone > 10:
    

相关问题 更多 >