I am trying to create a function that takes in a number and compares it to that same number in a txt file. If there is a number, output that entire line, else output that there is no number. Currently, there are only 9 lines.
My txt file (called nbaPointsLeaders.txt) is as follows
1. James Harden, Houston Rockets, 30.4
2. Anthony Davis ,New Orleans Pelicans, 28.1
3. LeBron James, Cleveland Cavaliers, 27.5
4. Damian Lillard, Portland Trail Blazers, 26.9
5. Giannis Antetokounmpo, Milwaukee Bucks, 26.8
6. Kevin Durant, Golden State Warriors, 26.4
7. Russell Westbrook, Oklahoma City Thunder, 25.4
8. Kyrie Irving, Boston Celtics, 24.4
9. LaMarcus Aldridge, San Antonio Spurs, 23.1
If the user inputs 1, input the entire first line.
Another problem I am running into is:
TypeError: 'in <string>' requires string as left operand, not int
and this comes up whenever I ask a user to input a number.
This is the code I have come up with so far. I am a beginner at python so it might be something obvious too.
def leaderInPoints(number):
number = int(input("What number from 1-9 would you like to see?"))
with open('nbaPointsLeader.txt','r') as file:
return [i for i in f if number in i])
Any help is greatly appreciated!
def leaderInPoints(number):
number = int(input("What number from 1-9 would you like to see?"))
file = open('nbaPointsLeader.txt','r')
# get all lines
content = file.readlines()
# we get the nth line
return content[number]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.