包含4年变量的If else语句,包括知道是哪一年

2024-06-17 04:55:49 发布

您现在位置:Python中文网/ 问答频道 /正文

所以问题是: 有1950-2050年,我们有奥运会和足球/足球世界锦标赛。 程序应该能够从这两个变量中分辨出它所基于的是哪一个,以及程序应该如何知道1950-2050年间的情况。 该程序还应该能够告诉什么时候没有比赛,例如2001年。你知道吗

我只是不知道如何解决这个问题。我在考虑定义价值观,比如

国际奥委会=(年份:1952196196019641968197219761980198419881992199620002004200820122016202042028203220362040204420448) Fifacup=(195019541958196219661970197419781982198619901994199820022006200102014201820222026203020342038204220462050)

也许我可以把这个程序编成一个测验 打印/输入(“1954年举办了哪届锦标赛?是世界杯还是奥运会然后可能有一些随机因素,使它在不同的年份之间随机,当人们键入错误的比赛,然后它说

打印错误的比赛。“再试一次”)

或者,这个程序可以理解年份和锦标赛之间的关系,并给出正确的答案,比如:

打印/输入(“写1950-2050年之间的一年:”和人写1982年 然后程序说 国际足联杯 如果错了呢 “错误的杯子”

也许我可以把这两个混合起来? 一些基于事实的程序和测验

你们怎么看?你知道吗

我不知道如何编码这将感谢所有的提示,我可以得到,但我想在这样的行

import random

Questions = int(input("How many questions do you want?"))

IOC = (1952,1956,1960,1964,1968,1972,1976,1980,1984,1988,1992,1996,2000,2004,2008,2012,2016,2020,2024,2028,2032,2036,2040,2044,2048)

Fifacup =(1950,1954,1958,1962,1966,1970,1974,1978,1982,1986,1990,1994,1998,2002,2006,2010,2014,2018,2022,2026,2030,2034,2038,2042,2046,2050)

IOC = random.randint(1,2050)

Fifacup = random.randint(1,2050)

while True:
Answer = int(input("What is "+str(IOC)+"+"+str(Fifacup)+"? "))
if answer == (IOC+Fifacup):
print ("Very good! Correct")

一切都很糟糕。我不知道,但我的代码是购物,这就好像没有一起工作,我有点尴尬,甚至张贴它。我真的很感激一些视频,是在解释这个功能的伟大!你知道吗


Tags: 程序input定义错误世界情况randomint
1条回答
网友
1楼 · 发布于 2024-06-17 04:55:49

代码有很好的文档记录,所以您知道每一行的作用,并且您有到资源的链接来了解我使用的东西。你知道吗

它的脚本使用起来非常乏味,每次你插入错误的东西它都会退出,这就是为什么我要做各种各样的事情,所以你没有完成所有的工作,你可以在这个过程中学习,它甚至有一个bug需要你在第一行代码中修复!你知道吗

import random
import sys

"""
In the case the user enters a non valid number we terminate the execution
TODO: ask again if the user doesn't introduce a valid integer
"""
try:
    number_of_questions = int(input('How many questions you want? '))
    if number_of_questions < 1:
        print("The number of questions must be greater than 0!")
except ValueError:
    print("The number of questions must be an integer!")
    sys.exit(1)

"""
You can read https://docs.python.org/3/library/stdtypes.html#range to know why you need the + 1 and that 
the range function can take up to 3 arguments: start, stop and step
"""

last_year = 2050 + 1
"""
We use list comprehensions because they are fast and a beautiful way to iterate over things
you can get more info about them here: https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions

We also use tuples insteand of lists, because tuples can't be mutated, and we dont want to change the years
"""
IOC = tuple(year for year in range(1952, last_year, 4))
FIFACUP = tuple(year for year in range(1950, last_year, 4))
"""
TODO: make this part without repeating the same text, and change the "was/will be" dymanically 
if you want to know how you can read about string interpolation or string formatting
"""

IOC_QUESTION = 'In which of this years was/will be the Olympics?'
FIFACUP_QUESTION = 'In which of this years was/will be the FIFA cup?'

answered_questions = 0
# We dont want an infinite bucle, so we will exit the program as soon as the user answered all the questions
while answered_questions < number_of_questions:
    subject = random.randrange(1, 2)  # we generate a number which can be 1 or 2
    if subject == 1:  # if the number is 1, we will ask about the olimpis
        print(IOC_QUESTION)
        correct_answer = random.choice(IOC)  # we pick a random valid answer
    if subject == 2:  # if the number is 2, we will ask about the fifa cup
        print(FIFACUP_QUESTION)
        correct_answer = random.choice(FIFACUP)  # we pick a random valid answer
    possible_answers = [
        correct_answer - random.randrange(1, 4),
        correct_answer + random.randrange(1, 4),
        correct_answer]  # we add and substract 1 to 3 years of the correct answer and save them in a tuple
    random.shuffle(possible_answers)  # we then shuffle the possible answers
    # We show the options for the user to pick
    answer = int(input(f'Choose one year in this list {possible_answers}:  '))
    # if the answer was not in the correct answers lists we show an error
    if answer not in possible_answers:
        """
        TODO: use string interpolation to show the answered value and the possibles values and
        ask again instead of exiting the program
        """
        print('You have to choose one of the offered answers!')
        sys.exit(1)
    if answer != correct_answer:
        # TODO: string interpolation and the try again
        print("That wasn't the correct answer!")
    if answer == correct_answer:
        print('Congratulations! that was the correct answer')  # TODO: string interpolation
    answered_questions += 1

print("You answered all the questions!")

相关问题 更多 >