본문 바로가기

function10.2

Python 파이썬으로 소수점 2자리로 반올림하는 방법은 무엇인가요? [중복], How to round to 2 decimals with Python? [duplicate] 질문 이 코드의 출력에서 많은 소수점을 얻고 있습니다 (화씨에서 섭씨로 변환). 현재 코드는 다음과 같습니다: def main(): printC(formeln(typeHere())) def typeHere(): global Fahrenheit try: Fahrenheit = int(raw_input("안녕하세요! 화씨 값을 입력하고 섭씨로 변환하세요!\n")) except ValueError: print "\n입력한 값이 숫자가 아닙니다!" print "화씨 값을 50으로 설정했습니다!" Fahrenheit = 50 return Fahrenheit def formeln(c): Celsius = (Fahrenheit - 32.00) * 5.00/9.00 return Celsius def printC(ans.. 2023. 9. 10.
Python 존재하지 않을 수도 있는 파일을 삭제하는 가장 파이썬다운 방법, Most pythonic way to delete a file which may not exist 질문 파일 filename이 존재하는 경우에 삭제하고 싶습니다. 이를 어떻게 말해야 할까요? if os.path.exists(filename): os.remove(filename) 더 좋은 방법은 있을까요? 한 줄로 처리할 수 있는 방법은 없을까요? 답변 더 파이썬스러운 방법은 다음과 같습니다: try: os.remove(filename) except OSError: pass 이는 더 많은 줄을 사용하고 매우 못생겨 보이지만, os.path.exists()를 불필요하게 호출하지 않으며 파이썬의 관습을 따릅니다. 이 작업을 수행하는 함수를 작성하는 것도 좋을 수 있습니다: import os, errno def silentremove(filename): try: os.remove(filename) excep.. 2023. 8. 5.