How to print the sum of looped user input?

In this code, I want to take all the inputs the user has entered until the condition of bat != sys_ball is false and sum them as score_board but I cannot figure out how to do it. Please help me with what code I should add so I can present score_board as a sum of all the bats inputted

import random

def batting ():         
  print("You can enter a number between 1 and 10")
  bat = int(input())
  sys_ball = random.randint(1,10)
  print (sys_ball)
  score_board = []

  while bat != sys_ball:
    batting ()

  print ("you win! Great Job!", scoreboard)
  oddeve()    

Upvotes: 0

Views: 73

Answers (2)

Devansu Yadav
Devansu Yadav

Reputation: 51

I think it is not possible to do with recursion but u can loop until the condition for while is false.Here's what i think another way to do it code could be:

import random
def batting():
    bat,sys_ball = 0,1
    sys_ball_lst = [i for i in range(1, 11)]
    score_board = []
    while bat != sys_ball:
        bat = int(input("You can enter a number between 1 and 10\n"))
        if bat not in sys_ball_lst:
            print("No not between 1 and 10")
            bat = int(input("You can enter a number between 1 and 10\n"))
        sys_ball = random.randint(1, 10)
        print(sys_ball)
        score_board.append(bat)
    print("you win! Great Job!", sum(score_board))
    oddeve()

You can enter a number between 1 and 10
5
4
You can enter a number between 1 and 10
5
3
You can enter a number between 1 and 10
5
6
You can enter a number between 1 and 10
5
6
You can enter a number between 1 and 10
5
1
You can enter a number between 1 and 10
5
8
You can enter a number between 1 and 10
2
2
you win! Great Job! 32

Upvotes: 0

azro
azro

Reputation: 54148

Don't do this using recursion, don't call the method again, the code should do it by itself.

You want to ask again until find it so code as it :

import random
def batting ():         
    bat, sys_ball = -1, 0
    score_board = []
    while bat != sys_ball:
        sys_ball = random.randint(1,10)
        bat = int(input("You can enter a number between 1 and 10:" ))
        print("Good" if bat == sys_ball else "Fail", "system choose", sys_ball)
        score_board.append(bat)

    print ("you win! Great Job!", sum(score_board))

That'll give the following

You can enter a number between 1 and 10:5
Fail system choose 4
You can enter a number between 1 and 10:4
Fail system choose 6
You can enter a number between 1 and 10:6
Fail system choose 10
You can enter a number between 1 and 10:5
Good system choose 5
you win! Great Job! 20

Upvotes: 3

Related Questions