基于输入的变量 [英] Variables based on input

查看:64
本文介绍了基于输入的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python版本= 3.5

Python Version=3.5

  1. 所以我想知道如何根据用户的输入来设置变量. 例如,如果用户要对此回答7:

  1. So I would like to know how I can set variables based on the input from the user. For example if a user was to answer 7 to this:

residents = input(您家有多少人?")

residents=input("How many people live at your house?")

EDIT =如果他们输入7,我怎么问每个人的名字?

EDIT= if they entered 7- how could I ask for the name of each individual??

谢谢!

推荐答案

def get_int(prompt):
    while True:
        try:
            return int(input(prompt))
        except ValueError:   # not an int!
            pass   # try again

residents = get_int("How many people live at your house? ")

,您可以使用列表:

resident_names = [input("Name of resident {}: ".format(i)) for i in range(1, residents + 1)]

这篇关于基于输入的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆