簡體   English   中英

開始 Python 抽獎功能

[英]Beginning Python Lottery Function

我正在嘗試創建一個函數,根據隨機生成的“獲勝者”序列評估用戶輸入。 代碼將運行良好,直到用戶輸入后,它才會停止。 我使用的編輯器也有點奇怪,所以縮進會在這里關閉,但是,我保證這不是問題。 對於那個很抱歉。 謝謝

from __future__ import print_function
import random
import sys

minimum, maximum = 1,69

def playPowerBall():
  instruction = "Please pick your {} number, between 1 and 69:"
  tickets = []
  for s in ('1st', '2nd', '3rd', '4th', '5th', '6th'):
    ticket = int(input(instruction.format(s)))
    tickets.append(ticket)

  range = (1,69)

  if any(ticket < minimum or ticket > maximum for ticket in tickets):
    print('One or more of the inputted numbers is not between 1-69. Please restart the function and try again.')
    sys.exit()

  winners = []

  for s in ('1st', '2nd', '3rd', '4th', '5th', '6th'):
    winner = random.sample(range(0,69), 6)
    winners.append(winner)

def matches(tickets, winners):
  score = 0

  for number in tickets:
    if number in winners:
      score += 1
    else:
      score += 0

    return score

  if 3 <= score:
    print('You picked at least three winning numbers, please claim your cash prize.')
  else:
    print('You do not have a winning combination. Would you like to play Powerball again? (Y/N)')
    response = str(input('Y/N:'))

    if response == 'Y':
      sys.restart()
    else:
      sys.exit()

您正在使用語句range = (1, 69)覆蓋內置函數range ,然后您執行winner = random.sample(range(0,69), 6)因此,您正在嘗試調用元組(1, 69) 如果刪除這樣的語句range = (0, 69) ,錯誤就會消失。 您的代碼中還有其他問題,您必須在playPowerBall結束時調用matches playPowerBall ,您必須從方法matches刪除return語句,並且sys沒有restart功能,但您可以遞歸調用playPowerBall

from __future__ import print_function
import random
import sys

minimum, maximum = 1,69

def playPowerBall():
  instruction = "Please pick your {} number, between 1 and 69:"
  tickets = []
  for s in ('1st', '2nd', '3rd', '4th', '5th', '6th'):
    ticket = int(input(instruction.format(s)))
    tickets.append(ticket)

  if any(ticket < minimum or ticket > maximum for ticket in tickets):
    print('One or more of the inputted numbers is not between 1-69. Please restart the function and try again.')
    sys.exit()

  winners = []

  for s in ('1st', '2nd', '3rd', '4th', '5th', '6th'):
    winner = random.sample(range(0,69), 6)
    winners.append(winner)

  matches(tickets, winners)

def matches(tickets, winners):
  score = 0

  for number in tickets:
    if number in winners:
      score += 1
    else:
      score += 0

  if 3 <= score:
    print('You picked at least three winning numbers, please claim your cash prize.')
  else:
    print('You do not have a winning combination. Would you like to play Powerball again? (Y/N)')
    response = str(input('Y/N:'))

    if response == 'Y':
      playPowerBall()
    else:
      sys.exit()

playPowerBall()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM