新手学python新手教程,现在遇到一个问题真的要吐血了!!!!完全不知道错在哪!

新手有问题 求大神解决一下 很着急的作业!!!!_python吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:62,299贴子:
新手有问题 求大神解决一下 很着急的作业!!!!收藏
我写的是一个猜数的游戏 当选择1开始游戏之后 第一次输的数直接被loop到后5次上了 而不是每次输一个数这是全部的代码
感觉问题出在def game(guess):
哪个大神要是能帮忙解决一下的话感激不尽import randomdef menu():
choice = input(&Enter a choice number: &)
while(True):
if choice == *1*:
print (&----------------------------------------------&)
secondmenu()
elif choice == *2*:
print (&----------------------------------------------&)
print (&quit&)
print (&----------------------------------------------&)
print (&Not a valid choice&)
choice = input(&Enter a choice number: &)def secondmenu():
print(&---------------------------------------------------------&)
print(&1.Play again&)
print(&2.Quit&)
choice = input(&Enter a choice number: &)
while(True):
if choice == *1*:
print (&----------------------------------------------&)
secondmenu()
elif choice == *2*:
print (&----------------------------------------------&)
print (&quit&)
print (&----------------------------------------------&)
print (&Not a valid choice&)def userin():
print (&This is a guessing number game!&)
print (&I will randomly select a integer number from 1 to 99.&)
print (&You have 6 chances to guess the number&)
print (&And you will get a hint after every guess&)
print (&---------------------------------------------------------------------------&)
print (&Let*s begin!!!&)
a=input(&Please guess the number: &)
number=int(a)
guess=int(a)
except ValueError:
print (&You didn*t enter a valid number&)
a=input(&Please guess the number: &)
game(guess)def game(guess):
secret=random.randint(1,100)
while guess != secret and tries & 6:
if guess & secret:
print (&====Your guess is too low !====\n&)
elif guess & secret:
print (&====Your guess is too high!====\n&)
tries=tries+1
print (&Above is your guess & + str(tries))
if guess == secret:
print (&Congratulations to you! Your
guess is right ! &)
print (&No more guesses! Better luck next time!&)
print (&The secret number was&,secret)
&&&User Menu&&&print (&Menu&)print (&-----------------------------------------------------&)print (&1.Start the guessing number game&)print (&2.Exit the game&)menu()
有人吗 大神在哪里
顶一顶 求帮助啊
太麻烦了写的
你game哪个def中,就没有主动要求用户输入任何东西啊。
你看看我帮你改的game这个函数,我帮你添加了一个用户输入的动作。至于后面的if guess == secret:后面的我没注意,没缩进。呵呵
改完之后第一个def userin:输入的值就无效了
我userin里面有一个input我这两个方程的想法是用def userin():来检验输入的数是不是可以 可以的话再带入def game
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或一、注意你的Python版本
Python官方网站为,当前最新版本为3.4.0 alpha,稳定版本为3.3.2,在3.0版本时,Python的语法改动较大,而网上的不少教程及语法针对的是1.0或者2.0版本的,这样就造成不少初学者按照示例代码来写,但编译都无法通过的问题。
1、print()函数
旧的print函数用法为print ‘Hello’,而新的print函数对此改成了print(‘Hello’)并且不再兼容之前版本。
如果在3.X版本上Python上使用旧的print语法,将出现“SyntaxError: invalid syntax”错误。
2、raw_input()与input()
Python3中用input()取代了raw_input(),当然这仅仅是重命名,使用上并没有不同;
3、比较符号,使用!=替换&&
4、repr函数
使用repr()函数替换``(注:反单引号,位于键盘1的左边一个键),将一个object转换为string,注意repr()与str()略有不同
5、exec()函数
exec用来执行存储在字符串或者文件中的Python语句,与JavaScript中的eval()函数类似,新的exec用法为exec(‘print(“Hello”)’)
二、新手常遇到的问题
1、如何写多行程序?
相信新手经常会遇到为何对着Python Shell发现程序没法换行,一换行就认为是执行了。这是因为你使用的是Python Shell!你可以点击File-&New Window或者Ctrl+N新开一个Python编辑器,这才是代码编辑器,尽情写你的Python程序吧,执行时只需要保存为后缀是.py的文件,然后F5就可以在Python Shell显示执行结果了。
2、如何执行.py文件?
直接双击.py文件即可,如果出现不能执行的问题,可能是你没有正确环境变量,在环境变量里找到Path,加上你Python的安装路径,比如C:\Python33\;
3、and,or,not
一般的编程语言比较关系运算符都是&&、||以及!,但Python偏偏使用and、or和not来分别代码并且、或者和非,我惊呆了。
4、True和False
没错,的确是True,而不是true,Python的这一点也实在令人难以理解,Python语法体系中基本都是小写的语法风格,为什么到这里要使用Pascal命名方式?
三、Python的数据结构
1、列表 List
声明方式:list=[1,2.3,’x’,'Hello’],拥有方法:
list.append(x) 在列表尾部添加一项
list.extend(L) 用给定的列表将当前列表接长,append与extend区别见
list.insert(i,x) 在给定的位置上插入项
list.remove(x) 移除列表中的第一个值为x的项,注意x并非索引
list.pop([i]) 删除给定位置的项并返回
list.index(x) 返回列表中第一个值为x的项索引值,没有匹配项则产生一个错误
list.count(x) 返回列表中x出现的次数
list.sort() 排序
list.reserve() 倒序
遍历示例:
numbers=[0,1,2,3,4,5,6,7,8,9]
for i in range(len(numbers)):
    print(i)
2、元组 Tuple
声明方式比较特殊:tuple=item1,item2,item3,例:
tuple=12,323.0,0.34,'Hello'
for i in range(len(tuple)):#遍历
    print(tuple[i])
3、集合 Set
声明方式:set={item1,item2,item3},例:
basket={'a’,'b','a','c','c’,'d'}
集合为无序不重复的元素集,上例声明的结果将为
{'d', 'a', 'b', 'c'}
遍历方式:
basket={'a','b','a','c','c','d'}
for i in basket:
    print(i)
4、字典 Dict
声明示例:
tel={'jack':23423,'sape':234}
可使用下述方式进行赋值:
tel['guido']=4127
结果为:{'sape': 234, 'guido': 4127, 'jack': 23423}
可使用items()方法取得键和对应的值,例:
for k,v in tel.items():
    print(k,v)
遍历方式:
tel={'jack':23423,'sape':234}
for key in tel:
    print(key ,':' , tel[key])
阅读(...) 评论()作为新手,我把之前遇到的问题贴出来
错误提示1:&TypeError: unbound method a() must be called with A instance as first argument (got nothing instead)
1 class A:
def a(self):
print("I'm a")
执行报错TypeError: unbound method a() must be called with A instance as first argument (got nothing instead)
表示没有对类进行实例, 改为:
1 class A:
def a(self):
print("I'm a")
1 class A:
@staticmethod
# 静态方法
print("I'm a")
一是通过def定义的 普通的一般的,需要至少传递一个参数,一般用self,这样的方法必须通过一个类的实例去访问,类似于c++中通过对象去访问;
二是在def前面加上@classmethod,这种类方法的一个特点就是可以通过类名去调用,但是也必须传递一个参数,一般用cls表示class,表示可以通过类直接调用;
三是在def前面加上@staticmethod,这种类方法是静态的类方法,类似于c++的静态函数,他的一个特点是参数可以为空,同样支持类名和对象两种调用方式;
在看一个例子:
1 class Person:
def __init__(self):
self.name='Flay'
self.count+=1
Person.count +=2
print(self.count)
print(Person.count)
10 if __name__ == '__main__':
p = Person()
#Person.name
因为self 是类本身属性, Person.count 表示count为类的静态属性如果直接Person.name 会直接报错:AttributeError: class Person has no attribute 'name'错误提示2:RuntimeError: super-class __init__() of type ani was never called
1 # -*- coding:utf8 -*-
2 from PyQt4.QtGui import *
3 import sys
6 class ani(QWidget):
def __init__(self):
self.resize(10, 20)
11 if __name__=='__main__':
app = QApplication(sys.argv)
window = ani()
window.show()
sys.exit(app.exec_())
报错原因:该类ani继承自QWidget,但没有给QWidget构造函数,如果类ani不继承任何基类可以这样:
1 class ani(object):
def __init__(self):
print('aaa')
6 if __name__=='__main__':
win = ani()
所以,创建了一个名为 ani 的新类, 该类继承 QWidget 类。 因此我们必须调用两个构造函数&&ani 的构造函数和继承类 QWidget 类的构造函数,代码改为如下:
1 from PyQt4.QtGui import *
2 import sys
4 class ani(QWidget):
def __init__(self):
super(ani, self).__init__()
self.resize(10, 20)
10 if __name__=='__main__':
app = QApplication(sys.argv)
window = ani()
window.show()
sys.exit(app.exec_())
错误3:You are using pip version 7.1.2, however version 8.1.2 is available.&You should consider upgrading via the 'python -m pip install --upgrade pip' comm&and.
&pip install *** 报错
解决办法:
C:\Python35\Scripts&easy_install.exe pip==8.1.2
&然后在安装包
C:\Python35&pip install C:\pypiwin32-219-cp35-none-win32.whl
阅读(...) 评论()&&国之画&&&&&&
版权所有 京ICP备号-2
迷上了代码!本人python新手,现在在看python基础教程,这几天敲了一下后面的项目5,出现了很奇怪的错误,大神帮看看_百度知道
本人python新手,现在在看python基础教程,这几天敲了一下后面的项目5,出现了很奇怪的错误,大神帮看看
希望大神们给看看,谢谢!!!python版本2.7.3错误如下:
error: uncaptured python exception, closing channel &__main__.ChatServer listening :5000 at 0xb74c820c& (&type 'exceptions.AttributeError'&:ChatSession instance has no attribute 'set_teminator' [/usr/lib/python2.7/asyncore.py|read|83] [/usr/lib/python2.7/asyncore.py|handle_read_event|443] [ChatServer.py|handle_accept|143] [ChatServer.py|__init__|104] [/usr/lib/python2.7/asyncore.py|__getattr__|421])error: uncaptured python exception, closing channel &__main__.ChatSession connected 127.0.0.1:35502 at 0xb70ef4ec& (&type 'exceptions.AttributeError'&:ChatSession instance has no attribute 'terminator' [/usr/lib/python2.7/asyncore.py|read|83] [/usr/lib/python2.7/asyncore.py|handle_read_event|449] [/usr/lib/python2.7/asynchat.py|handle_read|124] [/usr/lib/python2.7/asynchat.py|get_terminator|100] [/usr/lib/python2.7/asyncore.py|__getattr__|421])Traceback (most recent call last):
File &ChatServer.py&, line 148, in &module&
try: asyncore.loop()
File &/usr/lib/python2.7/asyncore.py&, line 216, in loop
poll_fun(timeout, map)
File &/usr/lib/python2.7/asyncore.py&, line 156, in poll
File &/usr/lib/python2.7/asyncore.py&, line 87, in read
obj.handle_error()
File &/usr/lib/python2.7/asyncore.py&, line 503, in handle_error
self.handle_close()
File &ChatServer.py&, line 128, in handle_close
self.enter(LogoutRoom(self.server))
File &ChatServer.py&, line 114, in enter
Room.add(self)TypeError: unbound method add() must be called with Room instance as first argument (got ChatSession instance instead)
提问者采纳
Room.add(self)这一句改成Roo哗户糕鞠蕹角革携宫毛m().add()当然,只是一个比喻,具体怎么样改,还要看你的程序。Room是一个类,除非它有特殊的方法。否则不能使用Room.add这样的方法,在python不让。你可以先实例化Room,比如Room(),然后在这个实例上就可以使用权Room().add()此外你Room.add(self)里的self显然不是Room实例,否则也许能成功。
我把代码改成Room().add()和room.add()但是还都是不对呢,您能帮忙看看代码么?
==这两天忙着跑步的事情。今天刚弄完。你很耐心。代码写得很棒。似乎你大小写弄错了。Room.add(self)改成room.add(self)应该就对了。
是114行的那个么,那个我改了还是不对,会不会是版本的问题啊?不过很谢谢您的回答!
加油。如果还解决不了,就尝试。或者是开启调试器,单步调到指定的位置,然后查看堆栈。 因为逻辑设置的太复杂,不太适合做单元测试。所以我们很少这样设计一个复杂的结构。 而是一开始先加一些逻辑,很多函数设为空,然后一步一步的加逻辑,加上一层就调试一下。这样速度反而快。
提问者评价
真的很感谢!
其他类似问题
为您推荐:
您可能关注的推广
python的相关知识
其他1条回答
调用的时候有问题吧。self这个参数不用自己写入参数中的,调用方法的时候。
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁}

我要回帖

更多关于 暗黑2新手不知道 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信