我想问用户他们的名字。
在给出他们的名字之后,它将被打印出来。
我想让用户在两个游戏之间进行选择,我想在if语句中调用正确的函数。
# coding=utf-8
import time
import calendar
from random import randint
def mygame():
"""
Guessing game!
"""
playing = True
num = randint(1, 100)
guesses = 0
print("Welcome to my Game")
print("Would you like to play?")
print("Yes or No")
Yes = "Yes"
No = "No"
x = input()
if x == Yes:
print("Welcome to my game!")
while playing:
print("Guess a number between 1 and 100")
guess = int(input("What is your guess?!"))
if guess > 100 or guess < 1:
invalid = True
while invalid:
print("Invalid number guessed, Enter a new NUMBER, between 1 and 100")
guess = int(input("What is your guess?!"))
guesses += 1
if 100 >= guess >= 1:
invalid = False
guesses -= 1
guesses += 1
print(guess)
if guess == num:
print("You Guessed the number correctly, it only took you " + str(guesses))
playing = False
elif guess > num:
print("Your guess was too high!, TRY AGAIN!")
else:
print("Your guess was too low, TRY AGAIN!")
if x == No:
print("Goodbye!")
quit()
def calendar1():
"""
This prints out calendar for November of 2016
"""
cal = calendar.month(2016, 11)
print("What is your name?")
name = input()
print("Hello %s, I have two options for you today!" % name)
localTime = time.asctime(time.localtime(time.time())) # This is formatted time!
print(localTime)
答案 0 :(得分:1)
您可以执行以下操作:
def game1():
# Code for first game goes here.
pass
def game2():
# Code for second game goes here.
pass
if __name__ == '__main__': # This is how you usually do it.
choice = input('Please select 1 for game1 and 2 for game2')
if choice == '1':
game1()
elif choice == '2':
game2()
else:
print('Please select a valid choice next time...!')