最喜欢的颜色

时间:2018-07-31 12:25:20

标签: python-3.x

$session_start();

extract($_POST);         
//extract data from submit post 

if(isset($submit))  
  {    
    if($user=="user" && $pass=="pass")    
      {     
        $_SESSION['user']= $user;       
        //if correct password and name store in session 
    } else {
        echo "Invalid user and password";
        header("Locatin:form.php")
    }
if(isset($_SESSION['user']))     
  {
  }

该代码旨在显示如下:

phonebook = {}

line = input('Name and colour: ')
while line:
  name, number = line.split()
  phonebook[name] = number
  line = input('Name and number: ')

for i in phonebook:
  print(i, phonebook[i])

我得到:

Name and colour: Nicky Blue

Name and colour: James Yellow

Name and colour: Sam Red

Name and colour: 

Sam Red

Nicky Blue

James Yellow

有人知道解决方法吗?

3 个答案:

答案 0 :(得分:1)

是正确的,只是在其他3行上显示 number 而不是 colour 只是一个拼写错误,而不是错误的代码。

脚本以以下内容开头: SELECT name, age, zip, jsonb_agg(ids ORDER BY ids) FROM ( SELECT name, age, zip, jsonb_array_elements(id) as ids FROM test) AS sub GROUP BY name, age, zip; name | age | zip | jsonb_agg ------+-----+-------+------------------------------------------ abc | 25 | 11111 | ["2722", "2722", "2855", "2855", "3583"]

,并在line = input('Name and colour: ')循环中指出:

while line

替换为:

  

line = input('Name and number: ')

反之亦然。

答案 1 :(得分:0)

如果要保留插入元素的顺序,则需要使用OrderedDict而不是字典对象:

from collections import OrderedDict

phonebook = OrderedDict()

答案 2 :(得分:0)

user = {}
line = input('Name and colour: ')
while line:
  name, colour = line.split()
  user[name] = colour
  line = input('Name and colour: ')
for i in user:
  print(i, user[i])