我想知道我的代码出了什么问题

时间:2019-06-08 04:11:57

标签: python python-3.x

import random

def coinToss():
    number = input("Number of times to flip coin: ")
    recordList = []
    heads = 0       
    tails = 0
for amount in range(number):
    flip = randint(0, 1)
    if: (flip == 0):
            print("Heads")
            recordList.append("Heads")
    else:
            print("Heads")
            recordList.append("Tails")
print(str(recordList))
print(str(recordList.count("Heads")) + str(recordList.count("Tails")))

我想知道这里出了什么问题。它表示if语句中存在语法错误。我只是想制作一个基本的硬币翻转应用程序。

我将我的错误放在这里:

{
    "resource": "/c:/Users/liamd/Documents/Programming 
    Files/Python/Cointoss.py",
    "owner": "python",
    "code": "syntax-error",
    "severity": 8,
    "message": "invalid syntax (<unknown>, line 10)",
    "source": "pylint",
    "startLineNumber": 10,
    "startColumn": 1,
    "endLineNumber": 10,
    "endColumn": 1
}

2 个答案:

答案 0 :(得分:0)

在“ if”之后删除“:”,仅将其放在行尾。 所以:

if (condition):
   do something
else:
   do something else

答案 1 :(得分:0)

我修复了您的代码,结果就出来了。

import random

def coinToss():
    number = input("Number of times to flip coin: ")
    recordList = []
    heads = 0
    tails = 0
    for amount in range(int(number)):
        flip = random.randint(0, 1)
        if flip == 0:
            print("Heads")
            heads += 1
            recordList.append("Heads")
        else:
            print("Tails")
            tails += 1
            recordList.append("Tails")
    return recordList

list = coinToss()

print("Heads: "+str(list.count("Heads")), "Tails: "+str(list.count("Tails")))

存在很多错误,在将其发布到Stack Overflow之前,您应该先进行调查