帮用python做个小程序

一个猜字游戏,效果是这样的:
Welcome to the Great CP1200 Guessing Game!
Written by Lindsay Ward, March 2011
What is your name? Jimi Clapton

Menu:
(I)nstructions
(P)lay Game
(Q)uit
>>> n
Invalid menu choice.

Menu:
(I)nstructions
(P)lay Game
(Q)uit
>>> i
I pick a number and you have to guess it.
Try and get it in as few guesses as possible.

Menu:
(I)nstructions
(P)lay Game
(Q)uit
>>> p
Please enter your guess, between 1 and 42: 0
Invalid guess.
Please enter your guess, between 1 and 42: 20
My number is lower.
Please enter your guess, between 1 and 42: 10
My number is higher.
Please enter your guess, between 1 and 42: 13
You got it!
Well done, Jimi Clapton. You guessed it in 3 guesses.

Menu:
(I)nstructions
(P)lay Game
(Q)uit
>>> Q
Thank you for playing.

我也是刚学python,下面是我的程序,试了下,大致没问题。
def print_title(seq):
seq_len = len(seq)
screen_width = 80
box_width = seq_len + 6
left_margin = (screen_width - box_width) // 2
print()
print(' '*left_margin + '+' + '-'*(box_width-2) + '+')
print(' '*left_margin + '|' + ' '*(box_width-2) + '|')
print(' '*left_margin + '|' + ' ' + seq + ' ' + '|')
print(' '*left_margin + '|' + ' '*(box_width-2) + '|')
print(' '*left_margin + '+' + '-'*(box_width-2) + '+')
print()

def continue_or_not():
while (True):
flag = input("continue?(Y/N)")
if (flag.lower() == 'y') : return True
elif (flag.lower() == 'n') : return False
else : print("Invalid input :", flag)

def show_menu():
print("Menu:")
print("(I)nstructions")
print("(P)lay Game")
print("(Q)uit")
print()

import random
def guess_num():
target_num = int( random.random()*42 ) + 1
count = 0
while (True):
guess_num = int( input("Please enter you guess(1---42): ") )
print("guess_num = ", guess_num)
if ( (guess_num < 1) | (guess_num > 42)) :
print("invalid guess")
count += 1
continue
if (guess_num == target_num) :
print("You got it!")
break
elif (guess_num < target_num) :
print("My number is higher")
count += 1
continue
else :
print("My number is lower")
count += 1
continue
return count

print_title("Welcome to the Great CP1200 Guessing Game!")
print("Written by Lindsay Ward, March 2011 ")
name = input("What is your name? ")
while (True):
show_menu()
choice = input()
if (choice.lower() == 'i'):
print("I pick a number and you have to guess it. ")
print("Try and get it in as few guesses as possible.")
print()
continue
elif (choice.lower() == 'p'):
guess_count = guess_num()
print("Well done!", name, "You guessed it in", guess_count, "guesses")
break
elif (choice.lower() == 'q'):
break
else :
print("Invalid Menu Choice")
print()
continue

print_title("Thanks for playing")
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-09-22
print "plz input a number:"
n=raw_input()
s=str(n)
print "n的位数是"+str( len(s) )
print "正序输出n:"
print n
print "逆序输出n:"
print int( s[::-1] )

思路就是先把数字转换为字符串,用字符串的操作来实现求位数和逆序,输出的时候再把字符串转为整数。
python的库很全,你想要的功能基本都有,不知道的查查书、手册,多去网上搜搜,学习学习变成自己的就行了。
相似回答