2014-04-13 2 views
0

현재 Python으로 프로그램을 작성 중입니다. 나는 약간의 패치를 다뤘다. 내가하려는 것은 간단합니다. 사용자가 프로그램에 입력 한 두 숫자의 차이를 계산합니다.Python에서 두 입력 변수의 차이를 얻는 방법

nameA = input("Enter your first German warriors name: ") 

print(nameA,"the noble.") 

print("What shall",nameA,"the noble strength be?") 

strengthA = input("Choose a number between 0-100:") 

print("What shall",nameA,"the noble skill be?") 

skillA = input("Choose a number between 0-100:") 

#Playerb 

nameB = input("Enter your first German warriors name: ") 

print(nameB,"the brave.") 

print("What shall",nameB,"the noble strength be?") 

strengthB = input("Choose a number between 0-100:") 

print("What shall",nameB,"the brave skill be?") 

skillB = input("Choose a number between 0-100:") 

사용자가 StrengthA와 StrengthB에 입력 한 값의 차이를 계산하려고합니다.

이 질문은 아마도 약간의 불쾌감을 줄 수 있습니다. 그러나 우리 모두는 배워야 만합니다. 감사합니다. .

답변

1

그냥 - 연산자를 사용하고 두 숫자의 차이를 얻으려면 abs()을 찾으십시오.

abs(StrengthA - StrengthB) 

그러나 정수로 작업해야합니다. 하여이 작업을 수행

StrengthA = int(input()) # Do the same with StrengthB. 

편집 :

는 당신이 단지 것, 그 전체 다섯으로 나눈 찾으려면 :

(abs(StrengthA - StrengthB))/5 
+0

이것은 지금까지 효과가 있습니다. 그렇다면이 모든 차이에 대해 어떻게 5로 나누는가? 도울 수 있니? – user3529441

+0

@ user3529441 답변 됨. –

+0

감사합니다. 알렉스. 죄송합니다. 나는 이것에 조금 새로운 것이다. :) – user3529441

0

코드 구조를 만들기위한 정말 좋은 당신의 이해하기 쉽고 유지하기 쉬운 프로그램 :

def get_int(prompt, lo=None, hi=None): 
    while True: 
     try: 
      value = int(input(prompt)) 
      if (lo is None or lo <= value) and (hi is None or value <= hi): 
       return value 
     except ValueError: 
      pass 

def get_name(prompt): 
    while True: 
     name = input(prompt).strip() 
     if name: 
      return name.title() 

class Warrior: 
    def __init__(self): 
     self.name  = get_name("Enter warrior's name: ") 
     self.strength = get_int("Enter {}'s strength [0-100]: ".format(self.name), 0, 100) 
     self.skill = get_int("Enter {}'s skill [0-100]: " .format(self.name), 0, 100) 

def main(): 
    wa = Warrior() 
    wb = Warrior() 

    str_mod = abs(wa.strength - wb.strength) // 5 

if __name__=="__main__": 
    main() 
관련 문제