Question

Python code question - you are given the grand prize(in USD) and the winning numbers. Your...

Python code question - you are given the grand prize(in USD) and the winning numbers.

Your program will read multiple tickets (6 selected numbers in each ticket) from the input file.

For each ticket, the program calculates its prize won, and outputs the prize (in USD) to the output file.

How to Play the Game?

In each ticket, the player selects five numbers from a set of 69 white balls (numbered 1 to 69)

and one number from 26 red balls (numbered 1 to 26).

The red ball number can be the same as one of the white balls, but a white ball can only be selected once.   

The order of selection does not matter.

The numbers in each ticket is not guaranteed to be sorted in any way.

Prize Rule:

The prize rule is given the figure below.

A ‘o means a white ball match and ‘x’ means the red ball match.

ooooox Grand Prize

ooooo $1,000,000

oooox $50,000

oooo     $100

ooox     $100

ooo       $7

oox       $7

ox          $4

x            $4

Input File: The input file is a text file (.txt).   

The first line contains only one number which is the grand prize.   

The second line contains exactly five numbers, separated by space characters, which are the five winning white balls.

The third line contains only one number which is the winning red ball.

Starting from the fourth line, each line contains exactly six numbers, separated by space characters, which represents a ticket.

The first five numbers are the selected white balls and the last number is the selected red ball.

You cannot assume how many tickets are in the input file.

No matter how many tickets are in the file, your program should read and process all of them.

The output file is a text file (.txt).

The first line contains a single number which is the calculated prize for the first ticket in the input file;

the second line contains a single number which is the calculated prize for the second ticket in the input file; and so on.

Since the prizes are represented in USD, you need to add a '$' before each number.

You can assume that the input file is error-free.

You do not need to consider how to deal with invalid inputs.

The input file is guaranteed to have no empty lines.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

def main():
winnigNumbers=[]
ticketNumbers=[]
# Open the file with read only permit
f = open('input.txt')
fw = open("output.txt", "w")
# use readline() to read the first line
grandPrice = f.readline().rstrip('\n\r')
lineNum=f.readline().split()
for num in lineNum:
winnigNumbers.append(int(num.rstrip('\n\r')))
winnigNumbers.append(int(f.readline().rstrip('\n\r')))
line= f.readline()
while line:
ticketNumbers=[]
lineNum=line.split()
countWhileMatchBall=0
for num in lineNum:
ticketNumbers.append(int(num.rstrip('\n\r')))
for i in range(len(ticketNumbers)-1):
for j in range(len(winnigNumbers)-1):
if(ticketNumbers[i]==winnigNumbers[j]):
countWhileMatchBall+=1
break
if(countWhileMatchBall==5 and ticketNumbers[5]==winnigNumbers[5]):
winningPrice=grandPrice
elif(countWhileMatchBall==5):
winningPrice="$1,000,000"
elif(countWhileMatchBall==4 and ticketNumbers[5]==winnigNumbers[5]):
winningPrice="$50,000"
elif(countWhileMatchBall==4):
winningPrice="$100"
elif(countWhileMatchBall==3 and ticketNumbers[5]==winnigNumbers[5]):
winningPrice="$100"
elif(countWhileMatchBall==3):
winningPrice="$7"
elif(countWhileMatchBall==2 and ticketNumbers[5]==winnigNumbers[5]):
winningPrice="$7"
elif(countWhileMatchBall==1 and ticketNumbers[5]==winnigNumbers[5]):
winningPrice="$4"
elif(ticketNumbers[5]==winnigNumbers[5]):
winningPrice="$4"
else:
winningPrice="$0"
fw.write(winningPrice+"\n")
line = f.readline()
f.close()
fw.close()
main()

input.txt file

output.txt

code snaps

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
Python code question - you are given the grand prize(in USD) and the winning numbers. Your...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT