python 함수 (아날로그 산수 게임)
3777 단어 python
from operator import add,sub
from random import randint,choice
ops={'+':add,'-':sub}
def init():
op=choice('+-')#
nums=[randint(1,10) for x in range(2)]
# nums.sort(reverse=True)
# *,
#
ans=ops[op](*nums)
pr='%d %s %d=' % (nums[0],op,nums[1])
oops=0;
while True:
try:
if int(input(pr))==ans:
print('correct')
break;
if oops==2:
print('answrer:
%s %d'% (pr,ans))
else :
print('incorrect... try again')
oops+=1
except(KeyboardInterrupt,EOFError,ValueError):
print('invalid input... try again')
def main():
while True:
init()
try:
opt=input('Again [y/n]?').lower()
if opt and opt[0]=='n':
break
except(KeyboardInterrupt,EOFError):
break
if __name__=='__main__':
main()
다음은 사전이 매개 변수로 함수를 호출하는 간단한 예이다
def test(**a):
for x in a:
print(x,':',a[x])
if __name__=='__main__':
a={'x':1,'y':2,'z':3}
test(**a)
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
로마 숫자를 정수로 또는 그 반대로 변환그 중 하나는 로마 숫자를 정수로 변환하는 함수를 만드는 것이었고 두 번째는 그 반대를 수행하는 함수를 만드는 것이었습니다. 문자만 포함합니다'I', 'V', 'X', 'L', 'C', 'D', 'M' ; 문자열이 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.