나는 코딩에 상당히 익숙하며, "Learn Python the Hard Way"라는 책을 사용하여 제 1 언어 (파이썬)를 배우고 있습니다. 그러나 이것은 책에서 특정한 연습이 아니며 단지 코드를 읽는 동안 연습하고 있습니다.가져온 파일에서 raw_input에서 정의 된 변수를 가져 오는 방법은 무엇입니까?
이def func():
a = float(raw_input("Enter Your Age:"))
b = float(raw_input("Enter Your Weight:"))
c = float(raw_input("Enter Your Height:"))
age = "a"
weight = "b"
height = "c"
if __name__ == "__main__":
print("This is pr1.py")
else:
print("%s is being imported to another file") % __name__
내 두 번째 파일은 pr2.py
입니다 : 운동 (23)와 현재 단지내 첫 번째 파일이 pr1.py입니다 ...이 경우에도 가능하면 알아 내려고 노력하고 있어요
import pr1 def func(): x = raw_input("Enter Your Race:") y = raw_input("Enter Your Gender:") z = raw_input("Enter Your Language:") print "Lets find your numbers" pr1.func() print "Lets find your identity" func() race = "x" gender = "y" language = "z" if __name__ == "__main__": print("This is pr2.py") else: print("%s is being imported into another file") % __name__
는에 "프린트"3 호선과 7 호선이 내가 무엇을 얻을 내가 pr3.py를 실행하고 스크립트를 주석 때이 내 3 파일 pr3.py
import pr1
print "%s = a %s = b %s = c" % (age, weight, height)
import pr2
print "%s = x, %s = y, %s = z" % (race, gender, language)
된다
python pr3.py
pr1 is being imported to another file
Lets find your numbers
Enter Your Age:25
Enter Your Weight:224
Enter Your Height:76
Lets find your identity
Enter Your Race:white
Enter Your Gender:male
Enter Your Language:english
pr2 is being imported into another file
그리고 pr3.py 파일에서 이전에 정의 된 변수와 함께 해당 명령문을 인쇄 할 것으로 기대합니다.
하지만 대신이 오류와 함께 제공 : 이제
pr1 is being imported to another file
Traceback (most recent call last):
File "pr3.py", line 3, in <module>
print "%s = a %s = b %s = c" % (age, weight, height)
NameError: name 'age' is not defined
내가 명령에서 마지막 파일을 실행할 때, 나는 그것이 이전이 개 파일을 가져올 것으로 기대하고, 그래서 입력 데이터는 내가 넣을 수 있습니다 raw_input에 입력 한 다음 다른 파일에서 사용하십시오 ...하지만 일단 두 파일을 가져 오면 각각의 raw_input에 데이터를 입력하는 것처럼 보입니다. pr3.py가 원시 입력과 해당 변수를 잊어 버린 것처럼 보입니다.
이 모든 것을 해결할 수있는 확실한 지식이 부족하다면 용서해주십시오.하지만 코딩에 익숙하지 않고 한 달 전에도 새로운 것을 알고 있습니다. 터미널에 디렉토리를 만들 수 있는지도 몰랐습니다.
감사합니다. 도움을 주셔서 감사합니다.
감사 정말 많은 도움이 된 깊이있는 설명을위한 시간! – Niles
@Niles 당신은 건강합니다. – EL3PHANTEN