帮助Python字符串乘法 [英] Help with Python string multiplying

查看:89
本文介绍了帮助Python字符串乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环顾四周但似乎找不到任何帮助。基本上我的问题是在代码的底部,行'score = score * 10'

当我在下一行打印时,它将得分返回为0。我想将它乘以10,因为十分中的分数需要转换为百分比。如果您对如何操作有任何其他想法,请提出建议。



Have looked around but cant seem to find any help. Basically my problem is at the bottom of the code with the line `score = score * 10'
it returns score as 0 when I print it in the next line. I want to multiply it by 10 as the score out of ten needs to be converted to a percentage. If you have any other ideas on how to do this please suggest them.

#This is where the import commands go
    from random import randint
    import random
     
    #This is a set list of variables for each question (21 in total as 2 for each question and operation)
    
    operations = ['x', '-', '+']
    operation = random.choice(operations)
    number1 = random.randrange(0,10)
    number2 = random.randrange(0,10)
    
    
    #This variable stores the users score and the correct answer for each question
    score = 0
    answer = None
    numberofquestions = 0
    
    
    
    """ THIS IS OLD CODE ********** IGNORE OLD CODE***********
    #This changes the randomly generated numbers into strings, then evauates them as maths equations so it knows the answers.
    #Also it stores the whole of each question as a variable, making it easier to code.
    question1 = eval(str(q1p1) + operation + str(q1p2))
    THIS IS OLD CODE ********** IGNORE OLD CODE***********"""
    
    
    
    #This asks for the user’s name
    
    name = input("What is your forename (With No Caps?)")
    surname = input("What is your surname (With No Caps?)")
    
    
    #This prints a welcome message, personalised with the user's name
    
    print("Welcome to your test," + name)
    
    
    #Information about how to answer questions
    
    print('''Throughout your test you will be asked a series of questions.
    These questions will be randomly generated and are designed to test your basic arithmetic skills.
    Please enter your answers in integer form.
    EG. if the question was 5 + 5, you would enter 10 with no spaces or extra characters.''')
    
    
    
    #First question
    while numberofquestions <10:
    	operations = ['x', '-', '+']
    	operation = random.choice(operations)
    	number1 = random.randrange(0,10)
    	number2 = random.randrange(0,10)
    	if operation == '+':
    		answer = number1 + number2
    	elif operation == '-':
    		answer = number1 - number2
    	elif operation == 'x':
    		answer = number1 * number2
    	user_answer = input("What is " + str(number1) + " " + operation + " " + str(number2) + "?")
    	user_answer = float(user_answer)
    	if user_answer == answer:
    		print("Well Done! You got it correct!")
    		score = score+1
    	else:
    		print("Sorry you got that wrong")
    	print ("***********-*-*-*-Your score so far is-*-*-*-*********** ")
    	print (score)
    	numberofquestions = numberofquestions+1
    print ("Well done, you have completed your test! Your final score was...")
    print (score)
    score = score * 10
    
    print ("Your score as a percentage is," +str(score))





我尝试了什么:



i尝试使用不同的编码百分比变量(得分)的方式玩耍,并且只是茫然地坐了10个分钟



What I have tried:

i have tried playing about with different ways of coding the percentage variable (score) and have just sat blankly for 10 minutes

推荐答案

您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



使用调试器确保python正确理解你的代码。

之前放置一个断点你计算百分比并检查每一个g是正确的。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

Use the debugger to ensure your code is understood correctly by python.
put a breakpoint just before you calc the percentage and check everything is correct.


这篇关于帮助Python字符串乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆