a number baseball program

Asked 2 years ago, Updated 2 years ago, 12 views

It's a number baseball program problem, but I have to use a defined function, so how do I do def over there?

import random    

c=["0","0","0"]
c[0] = str(random.randrange(1,9,1))
c[1] = c[0]
c[2] = c[0]
while (c[0] == c[1]):
    c[1] = str(random.randrange(1, 9, 1))
while (c[0] == c[2] or c[1] == c[2]):
    c[2] = str(random.randrange(1, 9, 1))

Attempt = 1 
strike = 0 
ball = 0 

print("I've chosen three numbers.")
print("Guess them!")

while ( strike < 3 ):

   num = str(input("Enter three numbers: "))

   strike = 0
   ball = 0

   for i in range(0, 3):
       for j in range(0, 3):
            if(num[i] == str(c[j]) and i == j):
                    strike += 1
            elif(num[i] == str(c[j]) and i != j):
                    ball += 1
   print (try), -th try: ", strike, "strike", ball, "ball\n")
   Attempt += 1

print("Congratulations! You hit!")

I know this is how you do it without using that's...

python

2022-09-20 16:35

1 Answers

I'm worried that your code is a little too similar to the code in the first document that comes out of Google for Python baseball game" but I'll pass that for now. Finding a starting point in someone else's code can be enough.

A function is to process and output inputs according to the rules.
You can create a function by just setting inputs, rules, and outputs.

It's okay to do this.

But I think I'll get in trouble if I add this and do my homework.
Let's cut and paste a little bit more.

Like this. I think we can cut this part separatelyCut out the parts you want to do little by little and remove them separately.

I think you can just define the input with 5:

It's not hard, is it? Try it.

PS. Some of the people who come here are asking questions that show no signs of effort two or three times and waiting for answers, saying, "I've run out of urgent programming homework, so give me an answer quickly." Some people are like that There are times when you get tired of answering questions. "Why are you doing this?" That's what I meant. I came to the site to post a question, and what's wrong? You wanted to, right? There's a story like that. I'm not the owner of this site, but I went over the topic. I'm sorry


2022-09-20 16:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.