简体   繁体   中英

Why is this program not running (Python)

I'm trying to write a program in Python that flips a coin and returns the longest series of heads and tails. It asks the user how many times to flip the coin. For some reason my program isn't running and I cant figure out why. I don't know why it isn't asking the user for the "number of flips" and "as a character string" for example.

import random

def flip():
    flipValue = random.randint(1,2)
    if flipValue == 1:
        side = "Heads"
    else:
        side = "Tails"
    return side

def nStreak():
    number = int(input("Number of flips: "))
    chars = int(input("As a character string: "))
    series = 0
    heads = 0
    tails = 0
    longest_h = 0
    longest_t = 0
    while series != number:
        side = flip()
        series += 1
        if side == "Heads":
            heads += 1
            tails = 0
            if heads == chars:
                longest_h += 1
                heads = 0
        if side == "Tails":
            tails += 1
            heads = 0
            if tails == chars:
                longest_t += 1
                tails = 0
    print("Number of heads streaks: ", longest_h)
    print("Number of tails streaks: ", longest_t) 

When I run it I get nothing.

You need to call the function at the last line.

nStreak()

Or else it won't execute the code.

在脚本的底部,添加没有标识的nStreak()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM