gpt4 book ai didi

python - 如何用字符串删除python中的括号?

转载 作者:行者123 更新时间:2023-12-01 22:30:59 25 4
gpt4 key购买 nike

我有这个程序:

import sys
students = []
grades = []

while True:
student = input ("Enter a name: ").replace(" ","")
if student.isalpha() == True and student != "0":
while True:
grade = input("Enter a grade: ").replace(" ","")
if grade == "0" or grade == 0:
print ("\n")
print ("A zero is entered.")
sys.exit(0)
if grade.isdigit()== True:
grade = int(grade)
if grade >= 1 and grade <= 10:
if student in students:
index = students.index(student)
grades[index].append(grade)
break
else:
students.append(student)
grades.append([grade])
break
else:
print("Invalid grade.")
elif student == "0":
print("A zero is entered.")
break
else:
print ("Invalid name.")
for i in range(0,len(students)):
print("NAME: ", students[i])
print("GRADE: ", grades[i])
print("AVERAGE: ", round(sum(grades[i])/len(grades[i]),1), "\n")

例如,当程序打印数字时,我需要让 [] 消失。

当您输入数字时,如下所示:

Enter a name: Jack
Enter a grade: 8
Enter a name: Jack
Enter a grade: 9
Enter a name: Jack
Enter a grade: 7
Enter a name: 0
A zero is entered.

打印出来是这样的:

NAME:  Jack
GRADE: [8, 9, 7]
AVERAGE: 8.0

但我需要程序像这样打印:

 NAME:  Jack
GRADE: 8, 9, 7
AVERAGE: 8.0

成绩应该没有括号。我想我需要使用字符串或其他东西,有人知道怎么做吗?

最佳答案

strip 允许您从开头和结尾删除指定的字符。

str(grades[i]).strip('[]')

关于python - 如何用字符串删除python中的括号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29525939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com