在python

时间:2018-10-24 02:35:19

标签: python python-3.x printing user-input

我认为我的任务已经差不多了。我正在尝试打印: “嗨,约翰,(新行) 您的程序设计原理占89.6%。”但是,当我打印以下内容时,它显示如下: “嗨,约翰,(新行)在您的编程课程中,您占89.4%。”
那么,你们可以帮助我如何在没有空格的情况下添加%符号吗?有没有更好的方法来打印这些单词?

student_name = input("Enter student's name: ")
course_name = input("Enter course name: ")

quizzes_weight = input("Enter quizzes weight: ")
projects_weight = input("Enter projects weight: ")
activities_weight = input("Enter activities weight: ")
attendance_weight = input("Enter attendance weight: ")
exams_weight = input("Enter exams weight: ")

quizzes_average = input("Enter quizzes average: ")
projects_average = input("Enter projects average: ")
activities_average = input("Enter activities average: ")
attendance_average = input("Enter attendance average: ")
exams_average = input("Enter exams average: ")

quizzes_weight = float(quizzes_weight)
projects_weight = float(projects_weight)
activities_weight = float(activities_weight)
attendance_weight = float(attendance_weight)
exams_weight = float(exams_weight)

quizzes_average = float(quizzes_average)
projects_average = float(projects_average)
activities_average = float(projects_average)
attendance_average = float(attendance_average)
exams_average = float(exams_average)

average_score_for_course = ((quizzes_weight * quizzes_average) + 
(projects_weight * projects_average) + (activities_weight * 
activities_average) + (attendance_weight * attendance_average) + 
(exams_weight * exams_average)) * 100

print("Hi",student_name +",","\nYou have a", 
average_score_for_course,"% in your", course_name,"course.")

对于此作业,我的上述编程应根据我的输入编码产生如下的输出。 例如:

Enter student's name: Ryan
Enter course name: Advanced Quantum Mechanics

Enter quizzes weight: .1
Enter projects weight: .2
Enter activities weight: .3
Enter attendance weight: .1
Enter exams weight: .3

Enter quizzes average: 1
Enter projects average: .85
Enter activities average: .9
Enter attendance average: .95
Enter exams average: .87

输出应该是这样的:

Hi Ryan,
You have a 89.6% in your Advanced Quantum Mechanics course.

4 个答案:

答案 0 :(得分:0)

您可以使用要打印的3个变量来格式化字符串:

public class TokenAuthorizationInterceptor implements ClientHttpRequestInterceptor {
    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
            throws IOException {
        request.getHeaders().add("ContentType", MediaType.APPLICATION_JSON);
        request.getHeaders().add("muh Header", "muh value");
        return execution.execute(request, body);
    }

}

this.restTemplate = new RestTemplate();
this.restTemplate.getInterceptors().add(new TokenAuthorizationInterceptor());

答案 1 :(得分:0)

查看您的代码:

print("Hi",student_name +",","\nYou have a", 
average_score_for_course,"% in your", course_name,"course.")

在打印功能中使用逗号分隔符时,默认情况下会添加一个空格。用+代替逗号:

print(average_score_for_course + "% in your")

通过用+替换逗号,它将删除Python默认添加的空间。

使用艾玛(Emma)的解决方案(字符串格式)是解决您问题的更好,更Python的解决方案。希望我能帮助您了解为什么会这样。

答案 2 :(得分:0)

只是一个不同的答案: 您也可以尝试

average_score_for_course = str(((quizzes_weight * quizzes_average) + 
(projects_weight * projects_average) + (activities_weight * 
activities_average) + (attendance_weight * attendance_average) + 
(exams_weight * exams_average)) * 100)+"%"

print("Hi",student_name +",","\nYou have a", 
average_score_for_course,"in your", course_name,"course.")

将结果更改为字符串,然后将“%”连接到字符串。

答案 3 :(得分:0)

print("Hi" + str(student_name) +"," + "\nYou have a" + str(average_score_for_course) +"% in your" + str(course_name) + "course.")

对我来说,我将尽量避免使用逗号,因为很难直观地看到您的打印效果,并且为了安全起见,最好在打印之前将“ average_score_for_course”转换为str。