请问怎么改啊

yhy3638 2008-12-15 02:05:38
from string import lower
from operator import add,sub,mul
from random import randint,choice
ops={'+':add,'-':sub,'*':mul}
MAXTRIES=2
def doprob():
op=choice('+ - *')
nums=[randint(1,10),randint(1,10)]
nums.sort();nums.reverse()
ans=apply(ops[op],nums)
pr='%d%s%s='%(nums[0],op,nums[1])
oops=0
while 1:
try:
if int(raw_input(pr))==ans:
print'correct'
break
if oops==MAXTRIES:
print 'sorry...the answer is \n%s%d'%(pr,ans)
else:
print'incorrect...try again'
oops=oops+1
except(KeyboardInterrupt,EOFError,
ValueError),diag:
print'invalid input...try again'
def main():
while 1:
doprob()
try:
opt=lower(law_input('Try another?([y]/n)'))
except(KeyboardInterrupt,EOFError):
print;break
if opt and opt[0]=='n':
break
if _name_== '_main_':
main()



Traceback (most recent call last):
File "D:/wayne/python/Lib/test.py", line 35, in <module>
if _name_== '_main_':
NameError: name '_name_' is not defined
请问怎么改啊
...全文
278 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
skyccm 2010-01-02
  • 打赏
  • 举报
回复
from string import lower
from operator import add,sub,mul
from random import randint,choice
ops={'+':add,'-':sub,'*':mul}
MAXTRIES=2
def doprob():
op=choice('+ - *')
nums=[randint(1,10),randint(1,10)]
nums.sort();nums.reverse()
ans=apply(ops[op],nums)
pr='%d%s%s='%(nums[0],op,nums[1])
oops=0
while 1:
try:
if int(raw_input(pr))==ans:
print'correct'
break
if oops==MAXTRIES:
print 'sorry...the answer is \n%s%d'%(pr,ans)
else:
print'incorrect...try again:'
oops=oops+1
except(KeyboardInterrupt,EOFError,
ValueError),diag:
print'invalid input...try again'
def main():
while 1:
doprob()
try:
opt=lower(raw_input('Try another?([y]/n)'))
if opt and opt[0]=='n':
break
except(KeyboardInterrupt,EOFError):
print;break

if __name__=='__main__':

main()

把你的代码执行一下,修改基本上都是和4楼的大同小异。4楼说了,我不必再说;
qap22 2008-12-15
  • 打赏
  • 举报
回复
不懂,up
king6o60 2008-12-15
  • 打赏
  • 举报
回复
修改之处共3个,第一个,双下滑,1楼时候我说过了。
第二个。op=choice('+-*'),而不是op=choice('+ - *'),这样它连空格都有可能会随机选到
第三个。raw_input而不是law_input


#!/usr/local/bin/python
# -*- encoding:utf-8 -*-
from string import lower
from operator import add,sub,mul
from random import randint,choice
ops={'+':add,'-':sub,'*':mul}
MAXTRIES=2
def doprob():
op=choice('+-*') ##here
nums=[randint(1,10),randint(1,10)]
nums.sort();nums.reverse()
ans=apply(ops[op],nums)
pr='%d%s%s='%(nums[0],op,nums[1])
oops=0
while 1:
try:
if int(raw_input(pr))==ans:
print'correct'
break
if oops==MAXTRIES:
print 'sorry...the answer is \n%s%d'%(pr,ans)
else:
print'incorrect...try again'
oops=oops+1
except(KeyboardInterrupt,EOFError,
ValueError),diag:
print'invalid input...try again'
def main():
while 1:
doprob()
try:
opt=lower(raw_input('Try another?([y]/n)')) ##here
except(KeyboardInterrupt,EOFError):
print;break
if opt and opt[0]=='n':
break
if __name__== '__main__': ##here
main()
yhy3638 2008-12-15
  • 打赏
  • 举报
回复
我是菜鸟阿。原来是这样还是有错误的
lixq2000 2008-12-15
  • 打赏
  • 举报
回复
同上
king6o60 2008-12-15
  • 打赏
  • 举报
回复
不知道你哪copy来的。记住是双下滑
if __name__ == "__main__":

37,742

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧