pythonell函数 python 函数

python 问题 需用三个以上functions,猜单词 , hangman gamedef getsecretword():
word="";
while not (word andword.islower()):
word=raw_input("Enter the secret word(all in lowercase):")
return word
def guessword(partword,word,times):
print("Word so far:%s"%partword)
ch=raw_input("Take guess number %d:"%times)
while len(ch)!=1:
ch=raw_input("Take guess number %d:"%times)
if ch not in word:
print("Sorry.")
else :
print("Got it!")
【pythonell函数 python 函数】index=word.find(ch)
partwordlist=list(partword)
while index=0:
partwordlist[index]=word[index]
index=word.find(ch,index 1)
partword=''.join(partwordlist)
if partword==word:
print("Congratulations.You correctly gueesed the word:%s"%word)
else:
guessword(partword,word,times 1)
def maingame():
word=getsecretword()
partword='*'*len(word)
guessword(partword,word,1)
def loopgame():
answer="y"
while answer in ['y','Y']:
maingame()
answer=raw_input("Do you want to play one more times?y/n?")
if __name__=='__main__':
loopgame()
用了四个函数
Python怎么拉平嵌套列表Python如何拉平(flatten)嵌套列表(nested list)
有时候会用到嵌套的列表(list),比如
[1, 2, [3, 4, [5, 6]], ["abc", "def"]]
如果将嵌套的列表拉平(flatten)呢?变成:
[1, 2, 3, 4, 5, 6, "abc", "def"]
方法有很多 , 目前了解到的各方面都比较好,也很pythonic的方法是:
def flatten(l):
for el in l:
if hasattr(el, "__iter__") and not isinstance(el, basestring):
for sub in flatten(el):
yield sub
else:
yield el
l = [1, 2, [3, 4, [5, 6]], ["abc", "def"]]
l2 = [x for x in flatten(l)]
print l2
#[1, 2, 3, 4, 5, 6, "abc", "def"]
python练习,不能使用find函数 , 命令中至少构建3个函数,要求python2.7版本,希望尽#coding=utf-8
'''
Created on 2014-11-03
@author: Neo
'''
def myFind(key, word):
flag = False
ret = []
for i in range(0, word.__len__()):
if key == word[i]:
flag = True
ret.append(i)
if flag:
return ret
else:
return flag
def setSecretWord():
word = raw_input("Enter the secret word(all in lowercase):")
ret = []
for i in range(0, word.__len__()):
ret.append(word[i])
return ret
def showWord(word):
show = ['*']*word.__len__()
i = 1
while True:
print "Word so far: %s" % ''.join(show)
key = raw_input("Take guess number %d:" % i)
flag = myFind(key, word)
if flag:
for k in range(0,flag.__len__()):
show[flag[k]] = key
print 'Got it.\r\n'
else:
print 'Sorry.\r\n'
i= 1
ret = myFind('*', ''.join(show))
if ret == False:
print "Congratulations. You correctly guessd the word: %s\r\n" % ''.join(word)
break
def main(again):
if again == 'y':
word = setSecretWord()
print "============================================"
showWord(word)
again = raw_input("Do you want to play one more time?y/n?")
main(again)
if __name__ == "__main__":
main('y')
运行结果:
Enter the secret word(all in lowercase):hello
============================================
Word so far: *****
Take guess number 1:e
Got it.
Word so far: *e***
Take guess number 2:s
Sorry.
Word so far: *e***
Take guess number 3:l
Got it.
Word so far: *ell*
Take guess number 4:h
Got it.
Word so far: hell*
Take guess number 5:o
Got it.
Congratulations. You correctly guessd the word: hello
Do you want to play one more time?y/n?y
Enter the secret word(all in lowercase):two
============================================
Word so far: ***
Take guess number 1:e
Sorry.
Word so far: ***
Take guess number 2:
..................
python字符串a为"hello",字符串b为"#2#lisaend" 。判断字符串b中是否含有“#2#原题:
字符串a为”Hello”,字符串b为”#2#Lisaend” 。判断字符串b中是否含有“#2#”,如果有,则将字符串a与字符串b中“#2#”与“end”之间的字符串用空格连接起来 , 然后输出 。输出“biubiubiu”
参考示例[python3]:
a = "Hello"
b = "#2#Lisaend"
if( "#2#" in b) :
i=b.find('end')
print(a ' ' b[3:i])
print("biu"*3)
基本教程:
Python 字符串
字符串是 Python 中最常用的数据类型 。我们可以使用引号('或")来创建字符串 。
创建字符串很简单,只要为变量分配一个值即可 。例如:
var1 = 'Hello World!'var2 = "Python Runoob"
Python 访问字符串中的值
Python 不支持单字符类型 , 单字符在 Python 中也是作为一个字符串使用 。
Python 访问子字符串,可以使用方括号来截取字符串,如下实例:
实例(Python 2.0 )
#!/usr/bin/python
var1 = 'Hello World!'var2 = "Python Runoob"
print "var1[0]: ", var1[0]print "var2[1:5]: ", var2[1:5]
以上实例执行结果:
var1[0]:H
var2[1:5]:ytho
Python 字符串连接
我们可以对字符串进行截取并与其他字符串进行连接,如下实例:
实例(Python 2.0 )
#!/usr/bin/python# -*- coding: UTF-8 -*-
var1 = 'Hello World!'
print "输出 :- ", var1[:6]'Runoob!'
以上实例执行结果
输出 :-Hello Runoob!
Python 转义字符
在需要在字符中使用特殊字符时,python 用反斜杠 \ 转义字符 。如下表:
转义字符
描述
\(在行尾时)续行符
\\反斜杠符号
\'单引号
\"双引号
\a响铃
\b退格(Backspace)
\e转义
\000空
\n换行
\v纵向制表符
\t横向制表符
\r回车
\f换页
\oyy八进制数 , yy代表的字符,例如:\o12代表换行
\xyy十六进制数,yy代表的字符,例如:\x0a代表换行
\other其它的字符以普通格式输出
Python字符串运算符
下表实例变量 a 值为字符串 "Hello",b 变量值为 "Python":
操作符
描述
实例
字符串连接
ab'HelloPython'
*重复输出字符串
a * 2'HelloHello'
[]通过索引获取字符串中字符
a[1]'e'
[ : ]截取字符串中的一部分
a[1:4]'ell'
in成员运算符 - 如果字符串中包含给定的字符返回 True
"H" in aTrue
not in成员运算符 - 如果字符串中不包含给定的字符返回 True
"M" not in aTrue
r/R原始字符串 - 原始字符串:所有的字符串都是直接按照字面的意思来使用,没有转义特殊或不能打印的字符 。
原始字符串除在字符串的第一个引号前加上字母"r"(可以大小写)以外,与普通字符串有着几乎完全相同的语法 。
print r'\n'\n print R'\n'\n
%格式字符串
pythonell函数的介绍就聊到这里吧 , 感谢你花时间阅读本站内容,更多关于python 函数、pythonell函数的信息别忘了在本站进行查找喔 。

    推荐阅读