几道Python的习题

搞着玩。

1。allVowels (inString)
Returns True if all characters in inString are vowels, False otherwise. Note that an empty
string is trivially all vowels.
要求是当输入的inString里的字母都是元音的话, 执行程序后显示TRUE ,不都是的话显示FALSE。
列子:
allVowels (inString)
>>> allVowels("a")
True
>>> allVowels("a eoa")
False
>>> allVowels("iaiaa")
True


 1  # !/usr/bin/python
 2  #  Filename: function1.py
 3 
 4  def  allVowels( inString ) :
 5      vowels  =   " aeiouAEIOU " ;
 6      ret  =  True;
 7       for  c  in  inString:
 8           if  c  not   in  vowels :
 9              ret  =  False;
10               break ;
11       return  ret;
12 
13 
14  print  allVowels( " a " )
15  print  allVowels( " a eoa " )
16  print  allVowels( " iaiaa " )



 
2。average(integerList)
Returns the mean value of the integers stored in the list integerList.
要求显示输入的一组数字的平均数
列子:
average(integerList)
>>> average([5,7,8])
6.666666666666667
>>> average([10])
10.0
>>> average([1,2,3])
2.0
 
 1  # !/usr/bin/python
 2  #  Filename: 2.py
 3 
 4  def  average( integerList ) :
 5          sum  =  0
 6           for  i  in  integerList :
 7              sum  +=  i
 8          num  =  len( integerList )
 9           if  num  >  0 :
10               return  sum  /  num 
11           else  :
12               return  None
13 
14 
15  print  average([ 5 , 7 , 8 ])
16  print  average([ 10 ])
17  print  average([ 1 , 2 , 3 ])
18  print  average( [] )
19 




3。toBinary (integerValue)
Returns string consisting of the binary representation of integerValue.
要求把输入的10进制数字转为2进制
列子:
toBinary (integerValue)
>>> toBinary(17)
'10001'
>>> toBinary(6)
'110'
>>> toBinary(0)
'0'
>>> toBinary(1012)
'1111110100'
 
 1  # !/usr/bin/python
 2  #  Filename: 3.py
 3 
 4  def  toBinary (integerValue) :    
 5      str  =   ""
 6      tryit  =  True
 7       while  tryit :
 8          i  =  integerValue  %   2 ;
 9           if  (i  ==  0) :
10              str  =   " 0 "   +  str;
11           else :
12              str  =   " 1 "   +  str;
13          integerValue  =  integerValue  //   2
14          tryit  =  integerValue  >  0;
15       return  str;
16 
17  print  toBinary( 17 )
18  print  toBinary( 6 )
19  print  toBinary(0)
20  print  toBinary( 1012 )



4。complexPalindrome(inString)
Returns True if inString is a complex palindrome, False if it it not. A complex palindrome is
a string that is the same forwards and backwards, ignoring spaces and capitalization.
要求如果输入的字母是对称的话 (大小写和空格忽略)显示TRUE, 不是的话显示FALSE。
列子:
complexPalindrome(inString)
>>> complexPalindrome("Aha")
True
>>> complexPalindrome("AHA")
True
>>> complexPalindrome("Race car")
True
>>> complexPalindrome("a")
True
 
 1  # !/usr/bin/python
 2  #  Filename: 4.py
 3 
 4  def  complexPalindrome(inString) :    
 5      i = 0
 6      j =- 1
 7      L  =  len(inString)
 8       while  i  -  j  <  L :
 9           if  (inString[i]  ==   "   " ) :
10              i  +=   1
11               continue
12           if  (inString[j]  ==   "   " ) :
13              j  -=   1
14               continue
15           if  inString[i].lower()  !=  inString[j].lower() :
16               return  False
17          i  +=   1
18          j  -=   1
19          
20       return  True
21      
22  print  complexPalindrome( " Aha " )
23  print  complexPalindrome( " AHA " )
24  print  complexPalindrome( " Race car " )
25  print  complexPalindrome( " a " )
26 



5。playGame()
Repeatedly issues intelligent guesses to the guessing game you built in tutorial. See below for
more detailed information.
In tutorial, you saw a game that two players play together - one player chooses the number and
the other player guesses it, and the computer tells the 2nd player if the guess was too high or too
low. Now, you will write a computer program that actually plays the game – it will make smart
guesses until it finds the right answer.
The Rules
1. The user will pick a number between 1 & 100 (but not tell the computer what it is)
2. The computer will make a guess
3. The user will tell the computer whether its guess was too high, too low, or right on
4. Steps 2 & 3 repeat until the computer guesses right, when it will output “I got it!”
要求是在0-100之间让电脑猜数字, 电脑猜了之后问你他猜的是大了还是小了, 你要输入大了小了还是对了, 一直到猜对为止。
列子:
playGame()
>>> game()
I guess 50
Am I too high (1), too low(2), or right on? (3)
1
I guess 25
Am I too high (1), too low(2), or right on? (3)
1
I guess 12
Am I too high (1), too low(2), or right on? (3)
2
I guess 18
Am I too high (1), too low(2), or right on? (3)
1
I guess 15
Am I too high (1), too low(2), or right on? (3)
2
I guess 16
Am I too high (1), too low(2), or right on? (3)
2
I guess 17
Am I too high (1), too low(2), or right on? (3)
3
I got it!


 1  # !/usr/bin/python
 2  #  Filename: 5.py
 3 
 4 
 5  def  playGame() :
 6      _min  =  0
 7      _max  =   100     
 8       while  True :
 9          num  =  (_min  +  _max)  //   2
10           print   " I guess  "   +  str(num)
11           print   " Am I too high(1), too low(2), or right on? (3) "
12          act  =  int(input())
13           if  act  ==   1  :
14              _max  =  num
15           elif  act  ==   2  :
16              _min  =  num
17           else  :
18               print   " I got it! "
19               break     
20 
21      
22      
23  playGame()
24 


转载于:https://www.cnblogs.com/belaliu/archive/2006/11/25/572140.html

猜你喜欢

转载自blog.csdn.net/weixin_33828101/article/details/93933218