python中的计算

时间:2014-10-28 14:02:07

标签: python

我发现很难用Python计算我的程序的一部分。

import random
CharacterOne= input("Enter the First players name:")
CharacterTwo= input("Enter the Second players name:")
ch1str=int(input(CharacterOne+" has a strength of:"))
ch1skl=int(input(CharacterOne+" has a skill of:"))
ch2str=int(input(CharacterTwo+" has a strength of:"))
ch2skl=int(input(CharacterTwo+" has a skill of:"))
strdif=int(ch1str-ch2str)
if strdif<0:
   strdif=ch2str-ch1str
strmod=strdif//5
skldif=int(ch1skl-ch2skl)
if skldif<0:
   skldif=ch2skl-ch1skl
sklmod=skldif//5

num1=random.randrange(1,6)
num2=random.randrange(1,6)
if num1>num2:
   ch1str+=strmod
   ch1skl+=sklmod
   ch2str-=strmod
   ch2skl-=sklmod
   play1 = True
if num2>num1:
   ch2str+=strmod
   ch2skl+=sklmod
   ch1str-=strmod
   ch1skl-=sklmod
   play1 = False
if ch1skl<0:
   ch1skl=0
if ch2skl<0:
   ch2skl=0
if ch1str=0:
   print(CharacterOne+" is now dead")
if ch2str=0:
   print(CharacterTwo+" is now dead")
if play1==True:
   print(CharacterOne+" won this round, now has",ch1str,"strength and",ch1skl,"skill")
   print(CharacterTwo+" lost this round, now has",ch2str,"strength and",ch2skl,"skill")
if play1==False:
   print(CharacterTwo+" won this round, now has",ch2str,"strength and",ch2skl,"skill")
   print(CharacterOne+" lost this round, now has",ch1str,"strength and",ch1skl,"skill")

这是代码,由于某种原因,当我输入两个角色的力量和技能时,程序打印我输入的内容并且实际上并没有使用str和skl mod和dif来计算新的力量和技能。

1 个答案:

答案 0 :(得分:0)

这里发生了很多事情,我不确定你是否了解你的逻辑实际上在做什么。在这种情况下,无论任何一个球员的力量或技巧,该回合的获胜者由随机数确定。另外,请查看有关条件的else和elif语句。

import random
import math
CharacterOne= input("Enter the First players name:")
CharacterTwo= input("Enter the Second players name:")
ch1str=int(input(CharacterOne+" has a strength of:"))
ch1skl=int(input(CharacterOne+" has a skill of:"))
ch2str=int(input(CharacterTwo+" has a strength of:"))
ch2skl=int(input(CharacterTwo+" has a skill of:"))

#you dont need to cast to int here, your vars are already ints. Also, you can use the absolute value to avoid the extra conditional.
strdif = abs(ch1str-ch2str)
strmod = strdif//5

skldif = abs(ch1skl-ch2skl)
sklmod = skldif//5

num1=random.randrange(1,6)

if num1>=3:
   ch1str+=strmod
   ch1skl+=sklmod
   ch2str-=strmod
   ch2skl-=sklmod
   play1 = True
else:
   ch2str+=strmod
   ch2skl+=sklmod
   ch1str-=strmod
   ch1skl-=sklmod
   play1 = False
#no idea what you're doing here. Do we care if their stats are in the negative? Why not just compare to <0?
#if ch1skl<0:
#   ch1skl=0
#if ch2skl<0:
#   ch2skl=0
#if ch1str<0:
#   ch1str=0
#if ch2str<0:
#   ch2str=0

if ch1str<0:
   print(CharacterOne+" is now dead")
if ch2str<0:
   print(CharacterTwo+" is now dead")

#Do you want the winner to be determined randomly? Currently your stats have no effect on the winner
if play1==True:
   print(CharacterOne+" won this round, now has",ch1str,"strength and",ch1skl,"skill")
   print(CharacterTwo+" lost this round, now has",ch2str,"strength and",ch2skl,"skill")
else:
   print(CharacterTwo+" won this round, now has",ch2str,"strength and",ch2skl,"skill")
   print(CharacterOne+" lost this round, now has",ch1str,"strength and",ch1skl,"skill")
相关问题