2016-12-01 63 views
-2

我想问用户的名字。两个游戏之间的选择

在给出它们的名字后,它将打印在一个句子中。

我想问用户在两个游戏之间进行选择,我想在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

你好。请重新格式化以提出具体问题。你试过什么,什么是/没有工作? – SummerEla

+0

我是一个初学者在Python和编程本身!基本上我试图做的是,有2个功能,并且可以说它们都包含游戏,猜谜游戏,数字游戏!我想要求用户输入在两个功能之间进行选择!当选择一个选择运行该功能,如果其他运行其他功能,你可以给我发电子邮件[email protected] –

+3

对不起,Edwin,这不是如何StackExchange的工作原理。我们都在这里帮助对方学习..不是代码给你。你为什么不自己尝试一下,让我们知道你卡在哪里? – SummerEla

回答

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...!')