我想在python 3.3.2中设置分数

时间:2013-08-18 19:20:48

标签: python-3.x counter

 **count = 0**
 player = input("Player Name: ")
 print("WELCOME TO MY QUIZ %s" % player, )
 print("Would You like to play the quiz ??")
 start = input()
 if start == "Yes" or start == "yes":
    print("Lets Start %s" % player, )
    print("Q1. What is the capital of India ?")
    print("A. Delhi")
    print("B. Mumbai")
    q1 = input()
    if q1 == "A":
         **count += 1**
    else:
         print("")
    print("Q2. How many states are there in India ?")
    print("A. 28")
    print("B. 29")
    q2 = input()
    if q2 == "B":
         count += 1
    else:
         print("")
    print("Q3. What is the capital of Maharashtra ?")
    print("A. Delhi")
    print("B. Mumbai")
    q3 = input()
    if q3 == "B":
        count += 1
    else:
        print("")
    ***print("You got"),str(count)+"/3 right!"***
else:
    print("Thank You, Goodbye")

到目前为止我已经做到了这一点,但我没有得到任何帮助的正确分数? 关于得分或计数,我得不到任何输出 我只得到“你有: 就是这样

3 个答案:

答案 0 :(得分:1)

您没有正确使用print()

使用

打印乐谱
print("You got {0}/3 right!".format(count))

答案 1 :(得分:0)

print("You got"), str(count)+"/3 right!"

是一个元组。 print("You got")是Python3中的函数调用;它打印到屏幕但返回None。 str(count)+"/3 right!"是一个字符串。两个表达式之间的逗号使组合表达式成为元组。您没有看到第二部分,因为它从未传递给print函数。 Python只是评估表达式,然后将其留给垃圾收集,因为它没有分配给任何东西。

因此,要以最小的更改修复代码,请移动括号并删除逗号:

print("You got" + str(count) + "/3 right!")

但是building strings with + is not recommended。 马特布莱恩特展示了首选方式。或者,由于您使用的是大于2.6的Python版本,因此您可以稍微缩短它:

print("You got {}/3 right!".format(count))

{}count取代。有关详细信息,请参阅The Format String Syntax


此外,而不是多次打印调用:

print("Lets Start %s" % player, )
print("Q1. What is the capital of India ?")
print("A. Delhi")
print("B. Mumbai")

您可以打印单个多行字符串:

print("""Lets Start {}
Q1. What is the capital of India ?
A. Delhi
B. Mumbai""".format(player))

更少的函数调用使其更快,并且更易读,并且需要更少的输入。

答案 2 :(得分:0)

我认为你是这样做的。 (不确定)

score = 0
ans = input('What is 2+2')
if ans == '4':
    print('Good')
    score = +1
else:
    print('Wrong')
    score = +0

显示分数这样做

print(score, 'Out Of 1')