Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

python - Convert CSV to XLSX using Openpyxl

I'm Trying to convert my guessing game scoreboard from CSV to Xlsx but I'm having trouble figuring out the coding for it, does anyone have any idea how to right out the code? I've tried looking up guides and info but I haven't figured anything out through resources so I figured I would try to ask some people instead.

This is the code for my game:

from random import randint       

MIN_GUESS=1                       
MAX_GUESS=100
FILENAME="Scoreboard.csv"
GREETING='''
* Welcome to our guessing game *  '''                     
FAREWELL='''*Thank you for playing our guessing game *
'''
import csv
import os


def playNewGame(gameList):
    name=input("Please enter your name: ")    
    numberToGuess=randint(MIN_GUESS,MAX_GUESS)    
    count=0
    while True:
        guess = int(input("Please make a guess: "))
        count+=1
        if guess > numberToGuess:
            print("Try a smaller number.")
        elif guess < numberToGuess:
            print("Try a larger number")
        else:
            print(f"You guessed the number in {count} tries.")
            break                           
    gameList.append([name,count,' '])
    return gameList

def evaluateBestScore(gameList): 
    minCount=10
    
    for game in gameList:   
        if int(game[1]) < minCount:        
           minCount=int(game[1])        
    for game in gameList:
        if int(game[1])==minCount:
            game[2] = "Best Score" 
        else:
            game[2] = ''
    return gameList


def askToPlayAgain():
    playAgain=input("Do you wish to play again?(Y/N)")
    if playAgain.upper() == 'Y':
        return True
    else:
        return False
    
def showScores(gameList):
    for game in gameList:
        print(game)

def main():         
   
    print(GREETING)
    keepPlaying=True
    gameList=[]
    while keepPlaying:
        gameList=playNewGame(gameList)
        keepPlaying=askToPlayAgain()
    gameList=evaluateBestScore(gameList)
    showScores(gameList)
    csvfile=open("Scoreboard.csv", "w", newline='')
    writer=csv.writer(csvfile,delimiter='',lineterminator="

")
    for listitem in gameList:
        writer.writerow(listitem)
    csvfile.close()
        
    print(FAREWELL)
    
    
main()
question from:https://stackoverflow.com/questions/65623020/convert-csv-to-xlsx-using-openpyxl

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...