40923235 cp2020

  • Home
    • Site Map
    • reveal
    • blog
  • 首頁
  • HW1
    • UNIT 1
      • Strategies for Learning學習策略
      • What You Need to Know About the Course您需要了解的課程內容
      • About these Materials 關於這些材料
      • Metacognition元認知
      • Metacognition in Action行動中的元認知
    • UNIT2
      • Introduction簡介
      • Functions of Computers  Input  Output  Storage  and Processing計算機功能  輸入  輸出  存儲和處理
      • Hardware硬件
      • Software軟件
      • Fireware固件
      • Componentization  Standardization組件化  標準化
      • Connection Interfaces and Cables連接接口和電纜
  • HW2
  • HW3
    • Chacrater input字符輸入
      • input strings types int輸入字符串類型int
      • Discussion討論區-1
      • User input in Python使用Python的用戶輸入
      • Manipulating strings a few ways處理字符串幾種方法
    • Odd Or Even奇數或偶數
      • Exercise 2 and solution練習2和解決方案
      • Discussion討論區-2
      • Modular arithmetic the modulus operator模塊化算術模運算符
      • Conditionals有條件的
      • Checking for equality and comparators in general檢查是否相等以及一般的比較器
    • List Less Than Ten列出少於十
      • Exercise 3 and Solution練習3和解決方案
      • Discussion討論區-3
      • Lists清單
      • More Conditionals更多條件
  • 心得
User input in Python使用Python的用戶輸入 << Previous Next >> Odd Or Even奇數或偶數

Manipulating strings a few ways處理字符串幾種方法

What you get from the input() function is a string. What can you do with it?

First: Make the string into a number. Let’s say you are 100% positive that the user entered a number. You can turn the string into an integer with the function int(). (In a later exercise or two or three there will be questions about what to do when the user does NOT enter a number and you try to do this; for now don’t worry about that problem). Here is what this looks like:

age = input("Enter your age: ")
age = int(age)

(or, if you want to be more compact with your code)

age = int(input("Enter your age: "))
	

In both cases, age will hold a variable that is an integer, and now you can do math with it.

(Note, you can also turn integers into strings exactly in the opposite way, using the str() function)

Second: Do math with strings. What do I mean by that? I mean, if I want to combine (concatenate is the computer science word for this) strings, all I need to do is add them:

	
print("Were" + "wolf")
print("Door" + "man")
print("4" + "chan")
print(str(4) + "chan")

The same works for multiplication:

print(4 * "test")

but division and subtraction do not work like this. In terms of multiplication, the idea of multiplyling two strings together is not well-defined. What does it mean to multiply two strings in the first place? However, it makes sense in a way to specify multiplying a string by a number - just repeat that string that number of times. Try this in your own program with all the arithmetic operations with numbers and strings - the best way to get a feel for what works and what doesn’t is to try it!

你從input()函數中得到的是一個字符串。你能做什麼呢?

第一:將字符串變成數字。假設你100%肯定用戶輸入了數字。你可以使用函數將字符串轉換為整數int()。(在以後的一兩三個練習中,當用戶不輸入數字而你嘗試這樣做時,將會出現有關如何處理的問題;現在不必擔心該問題)。看起來是這樣的:

age = input("Enter your age: ")
age = int(age)

(或者,如果你想使代碼更緊湊)

age = int(input("Enter your age: "))
	

在這兩種情況下,age都將包含一個整數變量,現在你可以使用它進行數學運算。

(注意,你也可以使用str()函數以相反的方式將整數轉換為字符串)

第二:對字符串進行數學運算。那是什麼意思 我的意思是,如果我想組合(連接起來就是計算機科學這個詞)字符串,我要做的就是添加它們:

	
print("Were" + "wolf")
print("Door" + "man")
print("4" + "chan")
print(str(4) + "chan")

乘法的工作原理相同:

print(4 * "test")

但是除法和減法不是這樣的。就乘法而言,將兩個字符串相乘的想法尚不明確。首先將兩個字符串相乘是什麼意思?但是,以某種方式指定將字符串乘以數字是有意義的-只需將該字符串重複該次數即可。在你自己的程序中嘗試使用帶有數字和字符串的所有算術運算-嘗試一下什麼有效和什麼無效的最佳方法是嘗試一下!


User input in Python使用Python的用戶輸入 << Previous Next >> Odd Or Even奇數或偶數

Copyright © All rights reserved | This template is made with by Colorlib