python中字符串乘法,帮助Python字符串乘法

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))

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

解决方案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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值