Python:仅选择输入的部分?

时间:2011-02-02 23:25:31

标签: python select random input

对不起......我是一个编程菜鸟。我在线查看了一些问题集,发现THIS ONE。我写了这么多:

import random

powerball=random.randint(1,42)

a=random.randint(1,53)
b=random.randint(1,53)
c=random.randint(1,53)
d=random.randint(1,53)
e=random.randint(1,53)

(f,g,h,i,j)=x=input("Your 5 Chosen Numbers:")

我的问题是,我不知道如果输入多于或少于五个,如何打印程序打印“请输入5个数字,仅用逗号分隔”。如果我希望它每次出错都会显示不同的消息,我该如何做呢?

3 个答案:

答案 0 :(得分:2)

尝试这种方法:

input_is_valid = False
while not input_is_valid:
    comma_separated_numbers = raw_input("Please enter a list of 5 numbers,separated by commas: ")
    numbers = [int(x.strip()) for x in comma_separated_numbers.split(",")]
    if len(numbers) != 5:
        print "Please enter exactly 5 numbers"
    else:
        input_is_valid = True

答案 1 :(得分:0)

我的主张:

import random
import sys

powerball=random.randint(1,42)

a=random.randint(1,53)
b=random.randint(1,53)
c=random.randint(1,53)
d=random.randint(1,53)
e=random.randint(1,53)


bla = ["\nPlease enter 5 numbers separated by only a comma : ",
       "\nPlease, I need 5 numbers separated by only a comma : ",
       "\nPLEASE, 5 numbers exactly : ",
       "\nOh gasp ! I said 5 numbers, no more nor less : ",
       "\n! By jove, do you know what 5 is ? : ",
       "\n==> I warn you, I am on the point to go off : "]

i = 0
while i<len(bla):

    x = raw_input(warn + bla[i])

    try:
        x = map(int, x.split(','))
        if len(x)==5:
            break
        i += 1

    except:
        print "\nTake care to type nothing else than numbers separated by only one comma.",

else:
    sys.exit("You wanted it; I go out to drink a beer : ")

(f,g,h,i,j)=x
print f,g,h,j,i

一些解释:

“table_list”中的

for_stmt :: =“for”target_list“:”套件               [“else”“:”套房]

在第一个套件中执行的 break 语句终止循环而不执行else子句的套件。在第一个套件中执行的continue语句会跳过套件的其余部分并继续下一个项目,如果没有下一个项目,则使用else子句。

http://docs.python.org/reference/compound_stmts.html#index-801

x = map(int,x.split(',')) 意味着函数 int()应用于iterable的每个元素,这是第二个参数。 这里的iterable是列表 x.split(',') 因此, x 是5个整数的列表 在Python 3中,不再有 raw_input(),它已被接收字符的 input()替换为 raw_input() Python 2。

答案 2 :(得分:0)

看看你的链接,我会说:

import random

while True:
    sets = input('how many sets? ')
    if type(sets) == int:
        break
    else:
        pass

for i in range(sets):
    ri = random.randint
    powerball = ri(1,42)
    other_numbers = sorted(ri(1,53) for i in range(5))
    print 'your numbers:','\t',other_numbers,'\t','powerball:','\t',powerball

似乎这或多或少都是他向你提出的要求。 如果我是正确的,你希望用户提交他的系列,看看是否提取了其中一组(amirite?)

然后可以这样做:

import random

while True:
    sets = input('how many sets? ')
    if type(sets) == int:
        break
    else:
        pass

while True:
    myset = raw_input('your 5 numbers:').split()
    if len(myset) != 5:
        print "just five numbers separated ny a space character!"
    else:
        myset = sorted(int(i) for i in myset)
        break


for i in range(sets):
    ri = random.randint
    powerball = ri(1,42)
    numbers = sorted(ri(1,53) for i in range(5))
    print 'numbers:','\t',numbers,'\t','powerball:','\t',powerball
    if numbers == myset:
        print "you won!" ##or whatever the game is about
    else:
        print "ahah you loser"

编辑:注意这不会检查随机生成的数字。因此,一个数字可以在同一序列中出现多次。练习你可以尝试避免这种行为,以慢节奏学习一些python的方式:

  1. 从“数字”列表的副本中创建一个集合 - 使用set()
  2. 如果长度小于5,则生成另一个数字
  3. 检查新号码是否在列表中
  4. 如果是,则将其附加到列表中。如果它不是唯一的,GOTO第1点: - )
  5. 再次排序整个事情
  6. 你去了
  7. 快乐阅读文档!