humble coin head or tail game script I wrote

Ant antroy at gmail.com
Sat Oct 7 09:57:13 CEST 2006


> # Get a list which contains 10 values from the user
> # let them predict Head Or Tail in ten times coin thrown
> # and then prdict the list by a fixed rule
>
>
> list = []
>
> print 'Predict Head or Tail in ten times coin thrown\nJust input \'h\'
> or \'t\' please\n'
>
> count = 0
> while True:
>     count += 1
>     print '\nNo.', count, ', h or t? '
>     pre_user = raw_input()
>     while pre_user != 'h' and pre_user != 't':
>         print '\njust enter \'h\' or \'t\' please'
>         print '\nNo.', count, ', h or t? '
>         pre_user = raw_input()
>     list.append(pre_user)
>     if count == 10:
>         break

You can simplify this considerably:

user_input = []  # list is a keyword so makes a bad variable name.

while len(user_input) < 10:
    print '\nNo.', len(user_input) + 1, ', h or t? '
    pre_user = raw_input()
    if pre_user not in ["h","t"]:   # Note you can also use <code>not
in "ht"</code>
        print '\njust enter \'h\' or \'t\' please'
        continue
    user_input.append(pre_user)

print user_input

HTH.




More information about the Python-list mailing list