jcalderin
jcalderin

Reputation: 75

Four digits from 0 - 9 are entered and generate a new combination of four (4) digits,

To see how you can help me, for a class I need to create a program in Python, which four digits from 0 - 9 are entered and generate a new combination of four (4) digits,

Example:

The number I enter,

1348

Based on that number I entered, the program create a new combination of number and add 1 only to their first number, 2348.

Then, generate a second number and add 1 only to the second digit, 1448.

Then generate third number and add 1 only to the third digit, 1358.

Then generate a fourth number and add 1 only to the fourth number m, 1349.

The program must only accept numbers and not more than four digits.

This is the code I created:

print """
-------------------------------------------------------
|                           Number Generator                                |
-------------------------------------------------------
"""
print 
winnum = raw_input("Enter last wining 4 numbers separated from 0 to 9 by spaces: ")
items = winnum.split()
lst2 = [eval(x) for x in items]
print 
print
print"1 number this week: ", lst2[0]+1,lst2[1],lst2[2],lst2[3]
print
print"2 number to play this week: ",lst2[0],lst2[1]+1,lst2[2],lst2[3]
print
print"3 number to play this week: ",lst2[0],lst2[1],lst2[2]+1,lst2[3]
print
print"4 number to play this week: ",lst2[0],lst2[1],lst2[2],lst2[3]+1 
print

Upvotes: 0

Views: 746

Answers (1)

Hugh Bothwell
Hugh Bothwell

Reputation: 56684

You skipped the part about "if the result when adding is greater than 9, return 0".

import sys

if sys.version_info[0] < 3:
    # Python 2.x
    inp = raw_input
else:
    # Python 3.x
    inp = input

def next_digit(i):
    return (i + 1) % 10

def main():
    prompt = "Please enter 4 digits separated by spaces: "
    a, b, c, d = [int(i) for i in inp(prompt).split()]

    print("Combo 1: {} {} {} {}".format(next_digit(a), b, c, d))
    print("Combo 2: {} {} {} {}".format(a, next_digit(b), c, d))
    print("Combo 3: {} {} {} {}".format(a, b, next_digit(c), d))
    print("Combo 4: {} {} {} {}".format(a, b, c, next_digit(d)))

if __name__ == "__main__":
    main()

Upvotes: 1

Related Questions